Order.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 小程序公共API服务
  7. */
  8. namespace app\bestbao\controller\api\v1;
  9. use app\common\facade\Inform;
  10. use app\bestbao\controller\api\Base;
  11. use app\bestbao\model\BestbaoEngineer;
  12. use app\bestbao\model\BestbaoOrder;
  13. use app\bestbao\model\BestbaoProduct;
  14. use app\bestbao\model\BestbaoProcess;
  15. use app\common\model\SystemUserAddress;
  16. use app\common\event\User as CommonUser;
  17. use app\common\facade\WechatPay;
  18. use app\common\facade\Upload;
  19. use filter\Filter;
  20. class Order extends Base{
  21. public function initialize() {
  22. parent::initialize();
  23. $this->isUserAuth();
  24. }
  25. /**
  26. * 我的订单
  27. * @return void
  28. */
  29. public function index(){
  30. $param = [
  31. 'page' => $this->request->param('page/d',0),
  32. 'types' => $this->request->param('types/d',0),
  33. 'sign' => $this->request->param('sign/s'),
  34. ];
  35. $rel = $this->apiSign($param);
  36. if($rel['code'] != 200){
  37. return enjson($rel['code'],'签名验证失败');
  38. }
  39. $condition['member_miniapp_id'] = $this->miniapp_id;
  40. $condition['uid'] = $this->user->id;
  41. $condition['state'] = $param['types'];
  42. $order = BestbaoOrder::where($condition)->field('id,title,update_time,urgent,state,engineer_id,phone,realname,address,engineer_id')->order('id desc')->page($param['page'],10)->select();
  43. if($order->isEmpty()){
  44. return enjson(0);
  45. }
  46. $data = $order->toArray();
  47. foreach ($data as $key => $value) {
  48. $data[$key]['update_time'] = date('Y-m-d H:i',$value['update_time']);
  49. $data[$key]['state_text'] = BestbaoOrder::stateText($value['state'],$value['engineer_id']);
  50. }
  51. return enjson(200,'操作成功',$data);
  52. }
  53. /**
  54. * 订单详情
  55. * @return void
  56. */
  57. public function reView(){
  58. $param = [
  59. 'id' => $this->request->param('id'),
  60. 'sign' => $this->request->param('sign/s'),
  61. ];
  62. $rel = $this->apiSign($param);
  63. if($rel['code'] != 200){
  64. return enjson($rel['code'],'签名验证失败');
  65. }
  66. $condition['member_miniapp_id'] = $this->miniapp_id;
  67. $condition['uid'] = $this->user->id;
  68. $condition['id'] = $param['id'];
  69. $order = BestbaoOrder::where($condition)->find();
  70. if(empty($order)){
  71. return enjson(0);
  72. }
  73. $order['update_time'] = date('Y-m-d H:i',$order['update_time']);
  74. $order['state_text'] = BestbaoOrder::stateText($order['state'],$order['engineer_id']);
  75. $order['imgs'] = json_decode($order['imgs'],true);
  76. $order['solve_time'] = empty($order['solve_time'])?'':date('Y-m-d H:i',$order['solve_time']);
  77. $info = BestbaoProcess::where(['order_id' => $param['id']])->select();
  78. $process = [];
  79. foreach ($info as $key => $value) {
  80. $process[$key]['text'] = $value['remarks'];
  81. $process[$key]['desc'] = date('Y-m-d H:i',$value['create_time']);
  82. }
  83. return enjson(200,'操作成功',['order'=>$order,'process' =>$process]);
  84. }
  85. /**
  86. * 提交维修订单
  87. * @return void
  88. */
  89. public function add(){
  90. if(request()->isPost()){
  91. $param = [
  92. 'order_product_id' => $this->request->param('order_product_id/d',0),
  93. 'urgent' => $this->request->param('urgent/d',0),
  94. 'title' => $this->request->param('title/s'),
  95. 'question' => $this->request->param('question/s',''),
  96. 'imgs' => $this->request->param('imgs/s','[]','htmlspecialchars_decode'),
  97. 'video' => $this->request->param('video/s','[]','htmlspecialchars_decode'),
  98. 'sound' => $this->request->param('sound/s','[]','htmlspecialchars_decode'),
  99. 'address' => $this->request->param('address/d',0),
  100. 'sign' => $this->request->param('sign/s'),
  101. ];
  102. $rel = $this->apiSign($param);
  103. if($rel['code'] != 200){
  104. return enjson($rel['code'],'签名验证失败');
  105. }
  106. $validate = $this->validate($param,'Order.order');
  107. if(true !== $validate){
  108. return enjson(403,$validate);
  109. }
  110. //判断是否允许提交
  111. $is_order = BestbaoOrder::where(['uid'=>$this->user->id,'state' => 1])->find();
  112. if($is_order){
  113. return enjson(301,'你还有欠费工单,请先把欠费工单结清.',['url'=>'/pages/order/review?id='.$is_order->id]);
  114. }
  115. //读取发货地址
  116. $address = SystemUserAddress::where(['user_id'=>$this->user->id,'id' =>$param['address']])->find();
  117. if(empty($address)){
  118. return enjson(403,'请选择收货地址');
  119. }
  120. $sound = Filter::filter_escape(json_decode($param['sound'],true));
  121. $video = Filter::filter_escape(json_decode($param['video'],true));
  122. //查询产品
  123. $product = BestbaoProduct::where(['id' =>$param['order_product_id']])->field('id')->find();
  124. $order['member_miniapp_id'] = $this->miniapp_id;
  125. $order['title'] = $param['title'];
  126. $order['question'] = $param['question'];
  127. $order['urgent'] = $param['urgent'];
  128. $order['uid'] = $this->user->id;
  129. $order['product_id'] = empty($product) ? 0 : $product->id;
  130. $order['imgs'] = json_encode(Filter::filter_escape(json_decode($param['imgs'],true)));
  131. $order['sound'] = empty($sound) ? '': $sound[0];
  132. $order['video'] = empty($video) ? '': $video[0];
  133. $order['realname'] = $address['name'];
  134. $order['phone'] = $address['telphone'];
  135. $order['address'] = $address['address'];
  136. $result = BestbaoOrder::create($order);
  137. if($result){
  138. BestbaoProcess::create(['member_miniapp_id' => $this->miniapp_id,'order_id' => $result->id,'engineer_id' => 0,'remarks' => '创建工单']);
  139. //通知到工程师
  140. $list = BestbaoEngineer::where(['member_miniapp_id' => $this->miniapp_id])->select()->toArray();
  141. foreach ($list as $info){
  142. Inform::sms($info['uid'],$this->miniapp_id,['title' =>'业务进展通知','type' => '新工单申请','content' =>'您有新的工单待接受','state' => '待收单']);
  143. }
  144. return enjson(200,'操作成功',['url'=>url('ask/index')]);
  145. }else{
  146. return enjson(0);
  147. }
  148. }
  149. }
  150. /**
  151. * 商城图片
  152. * @return void
  153. */
  154. public function upImg(){
  155. if(request()->isPost()){
  156. $rel = Upload::index();
  157. if($rel['error'] == 0){
  158. return json(['code'=>200,'msg'=>'success','data' => $rel['url']]);
  159. }else{
  160. return json(['code'=>204,'msg'=>'error']);
  161. }
  162. }
  163. }
  164. /**
  165. * @return \think\response\Json
  166. * @throws \think\exception\DbException
  167. * 解决方案订阅
  168. * */
  169. public function doBuy(){
  170. $param['sign'] = $this->request->param('sign');
  171. $param['id'] = $this->request->param('id');
  172. $rel = $this->apiSign($param);
  173. if($rel['code'] != 200){
  174. return enjson($rel['code'],'签名验证失败');
  175. }
  176. $condition['member_miniapp_id'] = $this->miniapp_id;
  177. $condition['uid'] = $this->user->id;
  178. $condition['id'] = $param['id'];
  179. $condition['state'] = 1;
  180. $order = BestbaoOrder::where($condition)->find();
  181. if(empty($order)){
  182. return enjson(403,'未找到需要付费的工单');
  183. }
  184. //唤醒微信支付参数
  185. $order->order_no = $this->user->invite_code.order_no(); //订单号
  186. $order->save();
  187. $payparm = [
  188. 'openid' => $this->user->miniapp_uid,
  189. 'miniapp_id' => $this->miniapp_id,
  190. 'name' => '支付工单费用',
  191. 'order_no' => $order->order_no,
  192. 'total_fee' => $order->price*100,
  193. 'notify_url' => api(1,'bestbao/notify/order',$this->miniapp_id),
  194. ];
  195. $ispay = WechatPay::orderPay($payparm);
  196. if ($ispay['code'] == 0) {
  197. return enjson(403,$ispay['msg']);
  198. }else{
  199. //通知到后台管理者微信
  200. Inform::sms(CommonUser::isFounder($this->miniapp_id)->user_id,$this->miniapp_id,['title' =>'业务进展通知','type' => '订单付款','content' =>'您有新的订单付款信息','state' => '已完成']);
  201. return enjson(200,'成功',$ispay['data']);
  202. }
  203. }
  204. //星级评价
  205. public function rate(){
  206. if(request()->isPost()){
  207. $param['sign'] = $this->request->param('sign');
  208. $param['id'] = $this->request->param('id');
  209. $param['rate'] = $this->request->param('rate/d',1);
  210. $rel = $this->apiSign($param);
  211. if($rel['code'] != 200){
  212. return enjson($rel['code'],'签名验证失败');
  213. }
  214. $condition['member_miniapp_id'] = $this->miniapp_id;
  215. $condition['uid'] = $this->user->id;
  216. $condition['id'] = $param['id'];
  217. $condition['state'] = 2;
  218. $order = BestbaoOrder::where($condition)->find();
  219. if(empty($order)){
  220. return enjson(403,'未找到需要评价的工单');
  221. }
  222. $order->rate = $param['rate']; //评级值
  223. $order->state = 3; //关闭订单
  224. $order->save();
  225. //进程
  226. BestbaoProcess::create(['member_miniapp_id' => $this->miniapp_id,'order_id' => $order->id,'engineer_id' => 0,'remarks' => '工单关闭']);
  227. //通知到工程师
  228. $engineer = BestbaoEngineer::where(['id' => $order->engineer_id])->find();
  229. if(!empty($engineer)){
  230. Inform::sms($engineer->uid,$this->miniapp_id,['title' =>'业务进展通知','type' => '订单评价','content' =>'您的订单有新的评价信息','state' => '已完成']);
  231. }
  232. return enjson(200,'成功');
  233. }
  234. }
  235. //读取订单数量
  236. public function statis(){
  237. $param['signkey'] = $this->request->param('signkey');
  238. $param['sign'] = $this->request->param('sign');
  239. $rel = $this->apiSign($param);
  240. if($rel['code'] != 200){
  241. return enjson($rel['code'],'签名验证失败');
  242. }
  243. //查询用户的工程师
  244. $engineer = BestbaoEngineer::where(['uid' => $this->user->id])->field('id,level,title')->count();
  245. $condition['member_miniapp_id'] = $this->miniapp_id;
  246. if($engineer){
  247. $condition['engineer_id'] = $this->user->id;
  248. }else{
  249. $condition['uid'] = $this->user->id;
  250. }
  251. $data = [];
  252. $data['all'] = BestbaoOrder::where($condition)->count();
  253. $data['progress'] = BestbaoOrder::where($condition)->where(['state' => 0])->count();
  254. $data['arrears'] = BestbaoOrder::where($condition)->where(['state' => 1])->count();
  255. return enjson(200,'成功',$data);
  256. }
  257. }