JsOrderController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2020-09-07
  6. * Time: 17:31
  7. */
  8. namespace api\js\controller;
  9. use Aliyun\DySDKLite\SignatureHelper;
  10. use api\js\model\JsModel;
  11. use api\js\model\JsOrderGrabModel;
  12. use api\js\model\JsOrderModel;
  13. use api\js\model\JsProjectModel;
  14. use api\js\model\JsScoreModel;
  15. use api\js\model\JsTimeModel;
  16. use api\js\model\PortalPostModel;
  17. use api\js\model\UserDiscountModel;
  18. use api\js\model\UserModel;
  19. use Cassandra\Date;
  20. use cmf\controller\RestUserBaseController;
  21. use JPush\Client;
  22. use JPush\Exceptions\APIConnectionException;
  23. use think\Db;
  24. use think\facade\Log;
  25. class JsOrderController extends RestUserBaseController
  26. {
  27. /**
  28. * @throws \think\Exception
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. * @throws \think\exception\DbException
  32. * @throws \think\exception\PDOException
  33. * 呼叫服务前的信息加载
  34. */
  35. public function getUserInfo()
  36. {
  37. $user_id = $this->getUserId();
  38. $UserModel = new UserModel();
  39. $projectId = $this->request->param('project_id');
  40. $storeId = $this->request->param('store_id');
  41. $district = $this->request->param('district');
  42. if (!$district) {
  43. $this->error('获取地址失败!');
  44. }
  45. $where['user_id'] = $user_id;
  46. $where['status'] = 0;
  47. $UserDiscountModel = new UserDiscountModel();
  48. $UserDiscountModel::where(function ($query) use ($user_id) {
  49. $query->where('user_id', $user_id);
  50. $query->where('status', 0);
  51. $query->where('past_time', '<', time());
  52. })->update(['status' => 2]);
  53. $userInfo = $UserModel::where('id', $user_id)->field('mobile,balance')->find();
  54. $userInfo['discount_count'] = $UserDiscountModel::where($where)->where(function ($query) use ($projectId) {
  55. if ($projectId) {
  56. $query->where('project_id', $projectId);
  57. $query->whereOR('project_id', 0);
  58. } else {
  59. $query->where('project_id', 0);
  60. }
  61. })->where(function ($query) use ($storeId) {
  62. if ($storeId) {
  63. $query->where('store_id', $storeId);
  64. $query->whereOR('store_id', 0);
  65. } else {
  66. $query->where('store_id', 0);
  67. }
  68. })->count();
  69. $userInfo['discount_info'] = [];
  70. $userDiscountInfo = $UserDiscountModel::with('jsDiscount')->where($where)->where(function ($query) use ($projectId) {
  71. if ($projectId) {
  72. $query->where('project_id', $projectId);
  73. $query->whereOR('project_id', 0);
  74. } else {
  75. $query->where('project_id', 0);
  76. }
  77. })->where(function ($query) use ($storeId) {
  78. if ($storeId) {
  79. $query->where('store_id', $storeId);
  80. $query->whereOR('store_id', 0);
  81. } else {
  82. $query->where('store_id', 0);
  83. }
  84. })->find();
  85. if ($userDiscountInfo) {
  86. $userInfo['discount_info'] = $userDiscountInfo;
  87. }
  88. $JsProjectModel = new JsProjectModel();
  89. $projectInfo = $JsProjectModel->getInfo($projectId, $district);
  90. $this->success('ok', ['user_info' => $userInfo, 'project_info' => $projectInfo]);
  91. }
  92. /**
  93. * @throws \think\Exception
  94. * 创建订单,并余额直接支付
  95. */
  96. public function addPost()
  97. {
  98. $data['user_id'] = $this->getUserId();
  99. $badness_num = UserModel::get($data['user_id'])['badness_num'];
  100. $user_nickname = UserModel::get($data['user_id'])['user_nickname'];
  101. $info = cmf_get_option('duration');
  102. if ($badness_num > $info['badness_num']) {
  103. $this->error('您有不良行为,不可下单!');
  104. }
  105. $data['user_discount_id'] = $this->request->param('user_discount_id');
  106. $data['service_id'] = $this->request->param('service_id');
  107. if (!$data['service_id']) {
  108. $this->error('服务区数据异常!');
  109. }
  110. $data['project_id'] = $this->request->param('project_id');
  111. $info = Db::name('js_project_service_relevancy')->where(['project_id' => $data['project_id'], 'id' => $data['service_id']])->find();
  112. if (!$info) {
  113. $this->error('该区未开通服务,不可下单!');
  114. }
  115. $data['price'] = $info['price'];
  116. $data['minute'] = $info['minute'];
  117. $data['order_price'] = $info['order_price'];
  118. $data['mobile'] = $this->request->param('mobile');
  119. if (!cmf_check_mobile($data['mobile'])) {
  120. $this->error('联系方式,手机号格式错误!');
  121. }
  122. $data['type'] = $this->request->param('type');
  123. $appoint_time = $this->request->param('appoint_time');
  124. if (!empty($appoint_time)) {
  125. $data['appoint_time'] = strtotime($appoint_time);
  126. }
  127. $data['province'] = $this->request->param('province');
  128. $data['city'] = $this->request->param('city');
  129. $data['district'] = $this->request->param('district');
  130. $data['address'] = $this->request->param('address');
  131. $data['address1'] = $this->request->param('address1');
  132. // $url = 'https://restapi.amap.com/v3/geocode/geo?key=3c813127122a372b7b0a6f0db7eb911e&address='. $data['province'].$data['city'].$data['district'].$data['address'].'&city='.$data['city'];
  133. // $res = curl_post_ssl1($url,'');
  134. // $result = json_decode($res,true);
  135. // if($result['status'] == 0){
  136. // $this->error('地址有误!');
  137. // }
  138. // $lng_lat = $result['geocodes'][0]['location'];
  139. // $lng = explode(',',$lng_lat)[0];
  140. // $lat = explode(',',$lng_lat)[1];
  141. $data['remark'] = $this->request->param('remark');
  142. $data['jiedan_js_id'] = $this->request->param('jiedan_user_id');
  143. $data['lat'] = $this->request->param('lat');
  144. $data['lng'] = $this->request->param('lng');
  145. $is_balance = $this->request->param('is_balance');
  146. $data['order_sn'] = cmf_get_order_sn() . $this->getUserId();
  147. $data['order_sole'] = md5(uniqid()) . md5(uniqid());
  148. $voucher = 0;
  149. if ($data['type'] == 1 && $data['jiedan_js_id']) {
  150. $week = date('w', time());
  151. $jsTime = JsTimeModel::where('js_id', $data['jiedan_js_id'])->find();
  152. $shangban = 0;
  153. if (in_array($week, $jsTime->weeks)) {
  154. foreach ($jsTime->times as $vo) {
  155. if (strtotime($vo['js_end_time']) > strtotime(date('H:i'))) {
  156. $shangban = 1;
  157. }
  158. // dump($vo['js_end_time']);
  159. }
  160. }
  161. if (!$shangban) {
  162. $this->error('技师休息中,不可下单');
  163. }
  164. }
  165. // 加钟类型
  166. if ($data['type'] == 3 && $data['jiedan_js_id']) {
  167. $data['jiedan_time'] = time();
  168. // 加钟技师佣金
  169. $data['order_price'] = round($info['price'] * 70 / 100, 2);
  170. }
  171. if ($data['user_discount_id']) {
  172. $userDiscountInfo = Db::name('user_discount')->where('id', $data['user_discount_id'])->find();
  173. if ($userDiscountInfo['past_time'] < time()) {
  174. $this->error('优惠券已过期!');
  175. }
  176. if ($userDiscountInfo['project_id'] != 0) {
  177. if ($userDiscountInfo['project_id'] != $data['project_id']) {
  178. $this->error('优惠券不适用该项目!');
  179. }
  180. }
  181. $voucher = Db::name('js_discount')->where('id', $userDiscountInfo['discount_id'])->value('voucher');
  182. }
  183. $data['voucher_price'] = $voucher;
  184. if ($voucher > $data['price']) {
  185. $this->error('优惠券不适用该项目!');
  186. }
  187. $data['pay_price'] = $data['price'] - $voucher;
  188. $JsOrderModel = new JsOrderModel();
  189. if ($is_balance == 1) {
  190. $userDalance = UserModel::get($this->getUserId())['balance'];
  191. if ($userDalance >= $data['pay_price']) {
  192. UserModel::where('id', $this->getUserId())->setDec('balance', $data['pay_price']);
  193. $data['status'] = 1;
  194. $data['pay_time'] = time();
  195. $data['pay_type'] = '余额支付';
  196. $JsOrderModel->allowField(true)->isUpdate(false)->save($data);
  197. $orderId = $JsOrderModel->id;
  198. $erweiam = qeCode($data['order_sole'], $this->request->controller(), $orderId);
  199. $JsOrderModel->allowField(true)->isUpdate(true)->save(['erweima' => $erweiam], ['id' => $orderId]);
  200. $log['user_id'] = $this->getUserId();
  201. $log['money'] = -$data['pay_price'];
  202. $log['order_sn'] = $data['order_sn'];
  203. $log['yue'] = $userDalance - $data['pay_price'];
  204. $log['type'] = 1;
  205. $log['remark'] = '呼叫服务余额支付';
  206. $log['add_time'] = time();
  207. $log['obj_id'] = $orderId;
  208. if ($log['money'] != 0) {
  209. Db::name('user_money_log')->insert($log);
  210. }
  211. Db::name('js_project')->where('id', $data['project_id'])->setInc('sales_num', 1);
  212. Db::name('user_discount')->where('id', $data['user_discount_id'])->update(['hours_time' => time(), 'status' => 1]);
  213. if ($data['jiedan_js_id']) {
  214. $helper = new SignatureHelper();
  215. $alisms = cmf_get_option('alisms');
  216. $security = false;
  217. $accessKeyId = $alisms['access_key_id'];
  218. $accessKeySecret = $alisms['access_key_secret'];
  219. $js_mobile = UserModel::get(JsModel::get($data['jiedan_js_id'])['user_id'])['mobile'];
  220. $params["PhoneNumbers"] = $js_mobile;
  221. $params["SignName"] = '嘀咚点到';
  222. if ($data['type'] == 1) {
  223. $params["TemplateCode"] = 'SMS_204986367';
  224. $params['TemplateParam'] = array(
  225. "product" => JsProjectModel::get($data['project_id'])['name'],
  226. );
  227. } else if ($data['type'] == 2) {
  228. $params["TemplateCode"] = 'SMS_204986363';
  229. $params['TemplateParam'] = array(
  230. "time" => $appoint_time,
  231. );
  232. }
  233. if (!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
  234. $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
  235. }
  236. $content = $helper->request(
  237. $accessKeyId,
  238. $accessKeySecret,
  239. "dysmsapi.aliyuncs.com",
  240. array_merge($params, array(
  241. "RegionId" => "cn-hangzhou",
  242. "Action" => "SendSms",
  243. "Version" => "2017-05-25",
  244. )),
  245. $security
  246. );
  247. }
  248. $this->success('支付成功', $JsOrderModel->id);
  249. } else {
  250. $this->error('余额不足');
  251. }
  252. } else {
  253. $JsOrderModel->allowField(true)->isUpdate(false)->save($data);
  254. $orderId = $JsOrderModel->id;
  255. $erweiam = qeCode($data['order_sole'], $this->request->controller(), $orderId);
  256. $JsOrderModel->allowField(true)->isUpdate(true)->save(['erweima' => $erweiam], ['id' => $orderId]);
  257. $this->success('创建订单成功', $JsOrderModel->id);
  258. }
  259. }
  260. /**
  261. * 用户结束服务
  262. */
  263. public function finish()
  264. {
  265. $ids = $this->request->param('order_id');
  266. if (empty($ids)) {
  267. $this->error('请选择要结束的订单');
  268. exit();
  269. }
  270. $JsOrderModel = new \app\back\model\JsOrderModel();
  271. JsOrderModel::where('id', $ids)->update(['finish_time' => time(), 'status' => 3]);
  272. $list = $JsOrderModel::where('id', $ids)->field('id,user_id,jiedan_js_id,order_price')->select();
  273. $info = cmf_get_option('jspus');
  274. foreach ($list as $vo) {
  275. $url = '/pages/ddxq/ddxq?id=' . $vo['id'];
  276. $jsuserid = $js_user_id = Db::name('js')->where('id', $vo['jiedan_js_id'])->value('user_id');
  277. \app\back\model\JsModel::where('id', $vo['jiedan_js_id'])->setInc('jiedan_num', 1);
  278. if ($vo['order_price'] > 0) {
  279. \app\back\model\UserModel::where('id', $jsuserid)->setInc('balance', $vo['order_price']);
  280. $log['user_id'] = $jsuserid;
  281. $log['money'] = $vo['order_price'];
  282. $log['type'] = 8;
  283. $log['remark'] = '完成服务获得金额';
  284. $log['add_time'] = time();
  285. $log['obj_id'] = $vo['id'];
  286. if ($log['money'] != 0) {
  287. Db::name('user_money_log')->insert($log);
  288. }
  289. }
  290. $js_user_id .= 'js';
  291. $js_client = new Client($info['js_app_key'], $info['js_master_secret']);
  292. $client = new Client($info['app_key'], $info['master_secret']);
  293. $extras = [
  294. 'id' => $vo['id'],
  295. 'flag' => '用户',
  296. 'url' => $url
  297. ];
  298. $array = [
  299. 'extras' => $extras,
  300. ];
  301. $text = '服务已被您指定结束';
  302. $platform = array('ios', 'android');
  303. $user_id = $vo['user_id'] . 'js';
  304. $client->push()
  305. ->setPlatform($platform)
  306. ->setNotificationAlert($text)
  307. ->iosNotification($text, $array)
  308. ->androidNotification($text, $array)
  309. ->addAlias("{$user_id}")
  310. ->send();
  311. $extras = [
  312. 'id' => $vo['id'],
  313. 'flag' => '技师端',
  314. 'url' => $url
  315. ];
  316. $array = [
  317. 'extras' => $extras,
  318. ];
  319. $text = '服务已被用户指定结束';
  320. $js_client->push()
  321. ->setPlatform($platform)
  322. ->setNotificationAlert($text)
  323. ->iosNotification($text, $array)
  324. ->androidNotification($text, $array)
  325. ->addAlias("{$js_user_id}")
  326. ->send();
  327. $this->fenxiao($vo['id'], 1);
  328. $this->fenxiao($vo['id'], 2);
  329. }
  330. $this->success('操作成功');
  331. }
  332. /**
  333. * 其他方式支付订单
  334. */
  335. public function pay()
  336. {
  337. $id = $this->request->param('order_id');
  338. $pay_type = $this->request->param('pay_type');
  339. switch ($pay_type) {
  340. case 'wxpay':
  341. $res = wx_pay($id, 3);
  342. if (!empty($res['return_code']) && $res['return_code'] == 'FAIL') {
  343. $this->error($res['return_msg']);
  344. } else {
  345. $this->success('ok', $res);
  346. }
  347. break;
  348. case 'alipay':
  349. $res = aly_pay($id, 3);
  350. if (!empty($res['return_code'])) {
  351. $this->error($res['return_msg']);
  352. } else {
  353. $this->success('ok', $res);
  354. }
  355. break;
  356. case 'balance':
  357. $orderInfo = JsOrderModel::get($id);
  358. $userDalance = UserModel::get($this->getUserId())['balance'];
  359. if ($orderInfo['status'] != 0) {
  360. $this->error('订单已支付!');
  361. }
  362. if ($userDalance >= $orderInfo['pay_price']) {
  363. $res = UserModel::where('id', $this->getUserId())->setDec('balance', $orderInfo['pay_price']);
  364. if ($res) {
  365. $data['status'] = 1;
  366. $data['pay_time'] = time();
  367. $data['pay_type'] = '余额支付';
  368. JsOrderModel::where('id', $id)->update($data);
  369. $orderId = $id;
  370. $log['user_id'] = $orderInfo['user_id'];
  371. $log['money'] = -$orderInfo['pay_price'];
  372. $log['yue'] = $userDalance - $orderInfo['pay_price'];
  373. $log['order_sn'] = $orderInfo['order_sn'];
  374. $log['type'] = 1;
  375. $log['remark'] = '呼叫服务余额支付';
  376. $log['add_time'] = time();
  377. $log['obj_id'] = $orderId;
  378. Db::name('user_money_log')->insert($log);
  379. Db::name('js_project')->where('id', $orderInfo['project_id'])->setInc('sales_num', 1);
  380. Db::name('user_discount')->where('id', $orderInfo['user_discount_id'])->update(['hours_time' => time(), 'status' => 1]);
  381. $this->success('支付成功', $orderId);
  382. }
  383. $this->error('余额支付失败,请重新支付');
  384. } else {
  385. $this->error('余额不足');
  386. }
  387. break;
  388. }
  389. }
  390. /**
  391. * 我的订单
  392. */
  393. public function getList()
  394. {
  395. $where = [];
  396. $JsOrderModel = new JsOrderModel();
  397. $status = $this->request->param('status');
  398. $project_name = $this->request->param('project_name');
  399. $user_id = $this->getUserId();
  400. $where['user_id'] = $user_id;
  401. if ($status || $status === '0') {
  402. if ($status == '9') $where['status'] = array('1', '2', '6');
  403. else $where['status'] = $status;
  404. }
  405. $where['user_delete'] = 1;
  406. $page = $this->request->param('page');
  407. $list = $JsOrderModel->getList($where, $page, $project_name, $status);
  408. $this->success('ok', $list);
  409. }
  410. /**
  411. * 获取抢单订单
  412. */
  413. public function getSeizeOrder()
  414. {
  415. $JsOrderModel = new JsOrderModel();
  416. $user_id = $this->getUserId();
  417. $where = ['user_id' => $user_id, 'status' => 1, 'jiedan_js_id' => 0];
  418. $list = $JsOrderModel->where($where)->select();
  419. $count = count($list->toArray());
  420. $fast_number = 0;
  421. foreach ($list as $vo) {
  422. $fast_number += $vo['fast_number'];
  423. }
  424. $this->success('获取成功', ['count' => $count, 'fast_number' => $fast_number]);
  425. }
  426. /**
  427. * 订单取消
  428. */
  429. public function orderCancel()
  430. {
  431. $id = $this->request->param('order_id');
  432. $JsOrderModel = new JsOrderModel();
  433. $log_file = './fencheng/cancel_' . date("YmdH") . '.txt';
  434. $orderInfo = $JsOrderModel::get($id);
  435. // if($orderInfo['status'] != 0 && $orderInfo['status'] != 1){
  436. // $this->error('该状态订单不可取消!');
  437. // }
  438. $timestamp = time();
  439. $waitTime = 15;
  440. $userDalance = UserModel::get($orderInfo['user_id'])['balance'];
  441. if ($orderInfo['status'] == '0') {
  442. JsOrderModel::destroy($id);
  443. } else if ($orderInfo['status'] == '1') {
  444. // 支付订单 15分钟内禁止取消订单
  445. // if ($timestamp - $orderInfo['pay_time'] < $waitTime * 60) {
  446. // $this->error("订单支付成功后,需等待 {$waitTime} 分钟才可取消订单!");
  447. // }
  448. $outMoney = $orderInfo['pay_price'];
  449. UserModel::where('id', $orderInfo['user_id'])->setInc('balance', $outMoney);
  450. $log['user_id'] = $orderInfo['user_id'];
  451. $log['money'] = $outMoney;
  452. $log['order_sn'] = $orderInfo['order_sn'];
  453. $log['yue'] = $outMoney + $userDalance;
  454. $log['type'] = 2;
  455. $log['remark'] = '取消服务支付金额退还到余额';
  456. $log['add_time'] = time();
  457. $log['obj_id'] = $id;
  458. Db::name('user_money_log')->insert($log);
  459. $JsOrderModel::where('id', $id)->update(['status' => 5]);
  460. } else if ($orderInfo['status'] == 2) {
  461. $withhold = Db::name('withhold')->where('id', 1)->find();
  462. if (time() - $orderInfo['jiedan_time'] > 30 * 60) {
  463. $withhold = Db::name('withhold')->where('id', 3)->find();
  464. } elseif (time() - $orderInfo['jiedan_time'] < 30 * 60 && time() - $orderInfo['jiedan_time'] > 15 * 60) {
  465. $withhold = Db::name('withhold')->where('id', 2)->find();
  466. }
  467. $refund_money = round($orderInfo['pay_price'] * ($withhold['deduct'] / 100), 2);
  468. $deserved_money = $orderInfo['pay_price'] - $refund_money;
  469. $refund_money = $refund_money - round(($orderInfo['pay_price'] * ($withhold['terrace'] / 100)), 2);
  470. $JsOrderModel::where('id', $id)->update(['status' => 5, 'return_time' => time()]);
  471. UserModel::where('id', $orderInfo['user_id'])->setInc('balance', $deserved_money);
  472. $log['user_id'] = $orderInfo['user_id'];
  473. $log['money'] = $deserved_money;
  474. $log['yue'] = $deserved_money + $userDalance;
  475. $log['order_sn'] = $orderInfo['order_sn'];
  476. $log['type'] = 2;
  477. $log['remark'] = '取消服务支付金额退还到余额';
  478. $log['add_time'] = time();
  479. $log['obj_id'] = $id;
  480. Db::name('user_money_log')->insert($log);
  481. if ($refund_money > 0) {
  482. $user_id = JsModel::get($orderInfo['jiedan_js_id'])['user_id'];
  483. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'user_id.......' . $user_id . "\n", FILE_APPEND);
  484. $userDalance = UserModel::get($user_id)['balance'];
  485. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'userDalance......' . $userDalance . "\n", FILE_APPEND);
  486. UserModel::where('id', $user_id)->setInc('balance', $refund_money);
  487. $log1['user_id'] = $user_id;
  488. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'user_id......' . $log1['user_id'] . "\n", FILE_APPEND);
  489. $log1['money'] = $refund_money;
  490. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'money......' . $log1['money'] . "\n", FILE_APPEND);
  491. $log1['yue'] = $refund_money + $userDalance;
  492. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'yue......' . $log1['yue'] . "\n", FILE_APPEND);
  493. $log1['order_sn'] = $orderInfo['order_sn'];
  494. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'order_sn......' . $log1['order_sn'] . "\n", FILE_APPEND);
  495. $log1['type'] = 6;
  496. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'type......' . $log1['type'] . "\n", FILE_APPEND);
  497. $log1['remark'] = '用户取消订单,接单补偿金额';
  498. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'remark......' . $log1['remark'] . "\n", FILE_APPEND);
  499. $log1['add_time'] = time();
  500. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'add_time......' . $log1['add_time'] . "\n", FILE_APPEND);
  501. $log1['obj_id'] = $id;
  502. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'obj_id......' . $log1['obj_id'] . "\n", FILE_APPEND);
  503. $log1['order_id'] = $id;
  504. @file_put_contents($log_file, date("Y-m-d H:i:s") . "\t" . 'order_id......' . $log1['order_id'] . "\n", FILE_APPEND);
  505. Db::name('user_money_log')->insert($log1);
  506. }
  507. }
  508. $JsOrderModel->allowField(true)->isUpdate(true)->save(['order_status' => 2], ['id' => $id]);
  509. $this->success('取消成功');
  510. }
  511. /**
  512. * 评价操作
  513. */
  514. public function setJsScore()
  515. {
  516. $id = $this->request->param('order_id');
  517. // $score = $this->request->param('score');
  518. $content = $this->request->param('content');
  519. $times = $this->request->param('times');
  520. $manner = $this->request->param('manner');
  521. $jishu = $this->request->param('jishu');
  522. $JsOrderModel = new JsOrderModel();
  523. $jiedan_js_id = $JsOrderModel::get($id)['jiedan_js_id'];
  524. $project_id = $JsOrderModel::get($id)['project_id'];
  525. // $js_id = JsModel::where('user_id',$jiedan_user_id)->value('id');
  526. $data['user_id'] = $this->getUserId();
  527. $data['order_id'] = $id;
  528. $data['js_id'] = $jiedan_js_id;
  529. $data['project_id'] = $project_id;
  530. $data['score'] = ceil(($times + $manner + $jishu) / 3);
  531. $data['content'] = $content;
  532. $data['times'] = $times;
  533. $data['manner'] = $manner;
  534. $data['jishu'] = $jishu;
  535. $JsScoreModel = new JsScoreModel();
  536. $JsScoreModel->allowField(true)->isUpdate(false)->save($data);
  537. $JsOrderModel::where('id', $id)->update(['status' => 4]);
  538. $this->success('评价成功');
  539. }
  540. /**
  541. * 订单详情
  542. */
  543. public function getInfo()
  544. {
  545. $id = $this->request->param('order_id');
  546. $order_no = $this->request->param('order_no');
  547. $call_id = $this->request->param('call_id');
  548. if ($call_id) {
  549. $info = Db::name('js_call')->where('id', $call_id)->find();
  550. if (!$info['agency_is_look']) {
  551. Db::name('js_call')->where('id', $call_id)->update(['agency_is_look' => 1, 'agency_is_look_time' => time()]);
  552. } else {
  553. if ((time() - $info['agency_is_look_time']) > 3600) {
  554. $this->error('信息不可查看!');
  555. }
  556. }
  557. }
  558. $JsOrderModel = new JsOrderModel();
  559. if (!$id && $order_no) {
  560. $id = $JsOrderModel::where('order_sn', $order_no)->value('id');
  561. }
  562. $orderInfo = $JsOrderModel::get($id, ['project', 'js', 'addtime' => function ($qyery) {
  563. $qyery->where(['status' => 1]);
  564. }]);
  565. $user_type = UserModel::get($this->userId)['user_type'];
  566. $orderInfo->distance = 0;
  567. if ($orderInfo['jiedan_js_id']) {
  568. $js_site_info = Db::name('js_site')->where('js_id', $orderInfo['jiedan_js_id'])->find();
  569. if ($js_site_info['lat'] && $js_site_info['lng'] && $orderInfo['lat'] && $orderInfo['lng']) {
  570. $orderInfo->distance = GetDistance($js_site_info['lat'], $js_site_info['lng'], $orderInfo['lat'], $orderInfo['lng'], 2, 2);
  571. } else {
  572. $orderInfo->distance = 0;
  573. }
  574. }
  575. if ($orderInfo->status == 5 || $orderInfo->status == 7 || $orderInfo->status < 2) {
  576. $orderInfo->mobile = substr_replace($orderInfo->mobile, '****', 3, 4);
  577. }
  578. if ($user_type == 3) {
  579. $js_id = JsModel::where('user_id', $this->userId)->value('id');
  580. $js_site_info = Db::name('js_site')->where('js_id', $js_id)->find();
  581. if ($js_site_info['lat'] && $js_site_info['lng'] && $orderInfo['lat'] && $orderInfo['lng']) {
  582. $orderInfo->distance = GetDistance($js_site_info['lat'], $js_site_info['lng'], $orderInfo['lat'], $orderInfo['lng'], 2, 2);
  583. } else {
  584. $orderInfo->distance = 0;
  585. }
  586. }
  587. $service = ['project_id' => $orderInfo['project_id'], 'minute' => $orderInfo['minute'], 'price' => $orderInfo['pay_price']];
  588. $orderInfo->project_service = $service;
  589. $orderInfo = $orderInfo->toarray();
  590. if ($orderInfo['status'] == 5 || $orderInfo['status'] == 7 || $orderInfo['status'] < 2) {
  591. $orderInfo['js']['phone'] = substr_replace($orderInfo['js']['phone'], '****', 3, 4);
  592. }
  593. // $orderInfo->project_service = Db::name('js_project_service_relevancy')->where('id',$orderInfo['service_id'])->find();
  594. $this->success('ok', $orderInfo);
  595. }
  596. /**
  597. * @throws \think\db\exception\DataNotFoundException
  598. * @throws \think\db\exception\ModelNotFoundException
  599. * @throws \think\exception\DbException
  600. * 获取抢单技师列表
  601. */
  602. public function selectJs()
  603. {
  604. $order_id = $this->request->param('order_id');
  605. if (!$order_id) {
  606. $this->error('缺少order_id参数');
  607. }
  608. $JsOrderGrab = new JsOrderGrabModel();
  609. $order_info = JsOrderModel::get($order_id);
  610. if (!$order_info) {
  611. $this->error('订单不存在!');
  612. }
  613. $list = $JsOrderGrab::with('js.site')->where('order_id', $order_id)->select();
  614. foreach ($list as $key => $value) {
  615. if ($list[$key]->js) {
  616. $list[$key]->js->score = get_js_score($list[$key]->js_id);
  617. }
  618. if ($value->js->site) {
  619. $list[$key]->distance = GetDistance($order_info['lat'], $order_info['lng'], $value->js->site->lat, $value->js->site->lng, 2, 2);
  620. } else {
  621. $list[$key]->distance = '距离不详';
  622. }
  623. }
  624. $paixu = 'asc';
  625. $len = count($list);
  626. for ($i = 1; $i < $len; $i++) {
  627. for ($k = 0; $k < $len - $i; $k++) {
  628. if ($paixu == 'asc' || $paixu == '') {
  629. if ($list[$k]['distance'] > $list[$k + 1]['distance']) {
  630. $array = $list[$k + 1];
  631. $list[$k + 1] = $list[$k];
  632. $list[$k] = $array;
  633. }
  634. } else {
  635. if ($list[$k]['distance'] < $list[$k + 1]['distance']) {
  636. $array = $list[$k + 1];
  637. $list[$k + 1] = $list[$k];
  638. $list[$k] = $array;
  639. }
  640. }
  641. }
  642. }
  643. $this->success('ok', $list);
  644. }
  645. /**
  646. * @throws \think\Exception
  647. * @throws \think\exception\PDOException
  648. * 用户选择技师操作
  649. */
  650. public function doSelectJs()
  651. {
  652. $order_id = $this->request->param('order_id');
  653. $js_id = $this->request->param('js_id');
  654. if (!$order_id) {
  655. $this->error('缺少order_id参数');
  656. }
  657. if (!$js_id) {
  658. $this->error('缺少js_id参数');
  659. }
  660. $JsOrderModel = new JsOrderModel();
  661. $order_info = JsOrderModel::get($order_id);
  662. if (!$order_info) {
  663. $this->error('订单不存在!');
  664. }
  665. $msg = '订单未支付!';
  666. switch ($order_info['status']) {
  667. case 2:
  668. $msg = '技师已选择!';
  669. break;
  670. case 6:
  671. $msg = '服务中!';
  672. break;
  673. case 3:
  674. $msg = '订单已完成!';
  675. break;
  676. case 4:
  677. $msg = '订单已评价!';
  678. break;
  679. case 5:
  680. $msg = '订单已取消!';
  681. break;
  682. }
  683. $order_id_new = $JsOrderModel::where(function ($query) {
  684. $query->whereOR('status', 2);
  685. $query->whereOR('status', 6);
  686. })->where('jiedan_js_id', $js_id)->value('id');
  687. if ($order_id_new) {
  688. $this->error('技师被选走了!');
  689. }
  690. if ($order_info['status'] != 1) {
  691. $this->error($msg);
  692. } else {
  693. $JsOrderModel->allowField(true)->isUpdate(true)->save(['jiedan_js_id' => $js_id, 'jiedan_time' => time(), 'status' => 2], ['id' => $order_id]);
  694. $JsOrderGrabModel = new JsOrderGrabModel();
  695. $JsOrderGrabModel->allowField(true)->isUpdate(true)->save(['status' => 1], ['order_id' => $order_id, 'js_id' => $js_id]);
  696. JsOrderGrabModel::where(['order_id' => $order_id, 'status' => 0])->delete();
  697. $info = cmf_get_option('jspus');
  698. $url = '/pages/ddxq/ddxq?id=' . $order_id;
  699. $js_client = new Client($info['js_app_key'], $info['js_master_secret']);
  700. $js_user_id = Db::name('js')->where('id', $js_id)->value('user_id');
  701. $js_user_id .= 'js';
  702. $extras = [
  703. 'id' => $order_id,
  704. 'flag' => '技师端',
  705. 'url' => $url
  706. ];
  707. $array = [
  708. 'extras' => $extras,
  709. ];
  710. $text = '您有新的服务订单,请及时处理!';
  711. $platform = array('ios', 'android');
  712. try {
  713. $js_client->push()
  714. ->setPlatform($platform)
  715. ->setNotificationAlert($text)
  716. ->iosNotification($text, $array)
  717. ->androidNotification($text, $array)
  718. ->addAlias("{$js_user_id}")
  719. ->send();
  720. $this->success('选择成功');
  721. } catch (\JPush\Exceptions\APIConnectionException $e) {
  722. $this->success('选择成功');
  723. } catch (\JPush\Exceptions\APIConnectionException $e) {
  724. $this->success('选择成功');
  725. }
  726. }
  727. }
  728. /**
  729. * 用户删除i订单
  730. */
  731. public function deleteOrder()
  732. {
  733. $order_id = $this->request->param('order_id');
  734. $JsOrderModel = new JsOrderModel();
  735. $JsOrderModel::where('id', $order_id)->update(['user_delete' => 2]);
  736. $this->success('删除成功');
  737. }
  738. /**
  739. * 扣款规则
  740. */
  741. public function getwithhold()
  742. {
  743. $list = Db::name('withhold')->select();
  744. $this->success('ok', $list);
  745. }
  746. /**
  747. * 加钟
  748. * 服务加时
  749. */
  750. public function addTime()
  751. {
  752. $order_id = $this->request->param('order_id');
  753. $pay_type = $this->request->param('pay_type');
  754. $info = JsOrderModel::get($order_id);
  755. $serve_info = Db::name('js_project_service_relevancy')->where('id', $info['service_id'])->find();
  756. if (!$serve_info) {
  757. $this->error('订单服务数据不存在了!');
  758. }
  759. if ($info['status'] != 6) {
  760. $this->error('订单不在服务中,不可加钟');
  761. }
  762. $data['pay_price'] = $serve_info['price'];
  763. $data['order_id'] = $info['id'];
  764. $data['order_sn'] = cmf_get_order_sn();
  765. $data['order_price'] = $serve_info['order_price'];
  766. $data['service_time'] = $serve_info['minute'];
  767. switch ($pay_type) {
  768. case 'wxpay':
  769. $order_addtime_id = Db::name('js_order_addtime')->insertGetId($data);
  770. if ($order_addtime_id) {
  771. $res = wx_pay($order_addtime_id, 100);
  772. if (!empty($res['return_code'])) {
  773. $this->error($res['return_msg']);
  774. } else {
  775. $this->success('ok', $res);
  776. }
  777. } else {
  778. $this->error('加钟失败!');
  779. }
  780. break;
  781. case 'alipay':
  782. $order_addtime_id = Db::name('js_order_addtime')->insertGetId($data);
  783. if ($order_addtime_id) {
  784. $res = aly_pay($order_addtime_id, 100);
  785. if (!empty($res['return_code'])) {
  786. $this->error($res['return_msg']);
  787. } else {
  788. $this->success('ok', $res);
  789. }
  790. } else {
  791. $this->error('加钟失败!');
  792. }
  793. case 'balance':
  794. $userDalance = UserModel::get($this->getUserId())['balance'];
  795. if ($userDalance >= $data['pay_price']) {
  796. UserModel::where('id', $this->getUserId())->setDec('balance', $data['pay_price']);
  797. $data['pay_time'] = time();
  798. $data['pay_type'] = '余额支付';
  799. $data['status'] = 1;
  800. $order_data['price'] = $data['pay_price'] + $info['price'];
  801. $order_data['pay_price'] = $data['pay_price'] + $info['price'];
  802. $order_data['minute'] = $data['service_time'] + $info['minute'];
  803. $order_data['order_price'] = $data['order_price'] + $info['order_price'];
  804. $JsOrderModel = new JsOrderModel();
  805. $JsOrderModel->allowField(true)->isUpdate(true)->save($order_data, ['id' => $order_id]);
  806. $orderId = $order_id;
  807. $log['user_id'] = $info['user_id'];
  808. $log['money'] = -$data['pay_price'];
  809. $log['yue'] = $userDalance - $data['pay_price'];
  810. $log['order_sn'] = $data['order_sn'];
  811. $log['type'] = 7;
  812. $log['remark'] = '加钟余额支付';
  813. $log['add_time'] = time();
  814. $log['obj_id'] = $orderId;
  815. Db::name('user_money_log')->insert($log);
  816. Db::name('js_project')->where('id', $info['project_id'])->setInc('sales_num', 1);
  817. Db::name('js_order_addtime')->insertGetId($data);
  818. // Db::name('')
  819. $this->success('加钟成功', $orderId);
  820. } else {
  821. $this->error('余额不足');
  822. }
  823. break;
  824. }
  825. }
  826. }