Dispatch.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use app\common\library\Sms as Smslib;
  6. /**
  7. * 派单接口
  8. */
  9. class Dispatch extends Common
  10. {
  11. protected $noNeedLogin = ['getSkillList','getReciveList'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 添加技能认证
  15. */
  16. public function addSkillAuth() {
  17. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  18. $leaver_id = $this->request->request('leaver_id',0,"intval"); // 技能等级ID
  19. $area_id = $this->request->request('area_id',0,"intval"); // 区服ID
  20. $image = $this->request->request('image'); // 图片说明
  21. $voice = $this->request->request('voice'); // 语音说明
  22. $voice_time = $this->request->request('voice_time'); // 声音时长
  23. $price = $this->request->request('price'); // 价格说明
  24. $remarks = $this->request->request('remarks'); // 技能介绍
  25. if (!$skill_id || !$image || !$voice || !$remarks || !$price) {
  26. $this->error(__('Invalid parameters'));
  27. }
  28. $is_main = 0;
  29. // 获取技能信息
  30. $skillModel = new \app\common\model\DispatchSkill();
  31. $skillInfo = $skillModel->getSkillInfo($skill_id);
  32. if(!$skillInfo) {
  33. $this->error(__('未找到技能信息!'));
  34. }
  35. $authModel = new \app\common\model\DispatchAuth();
  36. // 是否已经认证过
  37. $where = [];
  38. $where["user_id"] = $this->auth->id;
  39. $where["skill_id"] = $skill_id;
  40. $authInfo = $authModel->where($where)->find();
  41. if($authInfo) {
  42. if($authInfo["status"] == 0) $this->error(__('您已提交过认证申请,请耐心等待审核!'));
  43. if($authInfo["status"] == 1) $this->error(__('您已经认证过了'));
  44. }
  45. if($authModel->where(["user_id"=>$this->auth->id])->count()<=0) {
  46. $is_main = 1;
  47. }
  48. // 添加认证信息
  49. $data = [];
  50. $data["user_id"] = $this->auth->id;
  51. $data["skill_id"] = $skill_id;
  52. $data["leaver_id"] = $leaver_id;
  53. $data["area_id"] = $area_id;
  54. $data["image"] = $image;
  55. $data["voice"] = $voice;
  56. $data["voice_time"] = $voice_time;
  57. $data["price"] = $price;
  58. $data["is_main"] = $is_main;
  59. $data["image"] = $image;
  60. $data["remarks"] = $remarks;
  61. $data["createtime"] = time();
  62. $res = $authModel->insertGetId($data);
  63. if($res) {
  64. $this->success('申请发送成功,请耐心等待审核!');
  65. } else {
  66. $this->error(__('网络错误,请稍后重试!'));
  67. }
  68. }
  69. /**
  70. * 判断当前技能是否已认证
  71. */
  72. public function skillIsAuth() {
  73. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  74. if (!$skill_id) {
  75. $this->error(__('Invalid parameters'));
  76. }
  77. $authModel = new \app\common\model\DispatchAuth();
  78. // 是否已经认证过
  79. $where = [];
  80. $where["user_id"] = $this->auth->id;
  81. $where["skill_id"] = $skill_id;
  82. $authInfo = $authModel->field("status")->where($where)->find();
  83. if($authInfo) {
  84. if($authInfo["status"] == 0) $this->error(__('当前技能已提交过认证申请,请耐心等待审核!'));
  85. if($authInfo["status"] == 1) $this->error(__('当前技能已经认证过了'));
  86. }
  87. $skillLevelInfo = \app\common\model\DispatchSkillLeaver::where(["skill_id"=>$skill_id])->find();
  88. $skillAreaInfo = \app\common\model\DispatchSkillArea::where(["skill_id"=>$skill_id])->find();
  89. $res = [];
  90. $res["level"] = $skillLevelInfo?1:0;
  91. $res["area"] = $skillAreaInfo?1:0;
  92. $this->success("没有认证过,可以去认证!", $res);
  93. }
  94. // /**
  95. // * 创建接单信息
  96. // */
  97. // public function addRecive() {
  98. // $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  99. // $back_image = $this->request->request('back_image'); // 背景图
  100. // $price = $this->request->request('price'); // 服务价格
  101. // $describe = $this->request->request('describe'); // 一句话描述
  102. //// $tags = $this->request->request('tags'); // 标签列表(中文半角逗号隔开)
  103. // if (!$skill_id || !$back_image || !$describe) {
  104. // $this->error(__('Invalid parameters'));
  105. // }
  106. // $reciveModel = new \app\common\model\DispatchRecive();
  107. // $where = [];
  108. // $where["user_id"] = $this->auth->id;
  109. // $where["skill_id"] = $skill_id;
  110. // $where["status"] = 1;
  111. // $reciveInfo = $reciveModel->where($where)->find();
  112. // if($reciveInfo) {
  113. // $this->error(__('您已存在当前技能的接单信息,请勿重复提单'));
  114. // }
  115. //
  116. // // 获取技能信息
  117. // $skillModel = new \app\common\model\DispatchSkill();
  118. // $skillInfo = $skillModel->getSkillInfo($skill_id);
  119. // if(!$skillInfo) {
  120. // $this->error(__('未找到技能信息!'));
  121. // }
  122. //
  123. // // 添加接单信息
  124. // $data = [];
  125. // $data["user_id"] = $this->auth->id;
  126. // $data["skill_id"] = $skill_id;
  127. // $data["back_image"] = $back_image;
  128. // $data["price"] = $price;
  129. // $data["unit"] = $skillInfo["unit"];
  130. // $data["describe"] = $describe;
  131. //// $data["tags"] = $tags;
  132. // $data["createtime"] = time();
  133. // $res = $reciveModel->insertGetId($data);
  134. // if($res) {
  135. // $this->success('恭喜,接单信息已发送!');
  136. // } else {
  137. // $this->error(__('网络错误,请稍后重试!'));
  138. // }
  139. // }
  140. // /**
  141. // * 创建订单
  142. // */
  143. // public function addOrder() {
  144. // $recive_id = $this->request->request('recive_id',0,"intval"); // 接单ID
  145. // $num = $this->request->request('num',0,"intval"); // 数量
  146. // $describe = $this->request->request('describe'); // 备注要求
  147. // if (!$recive_id || !$num || !$describe) {
  148. // $this->error(__('Invalid parameters'));
  149. // }
  150. //
  151. // // 获取接单信息
  152. // $reciveModel = new \app\common\model\DispatchRecive();
  153. // $where = [];
  154. // $where["id"] = $recive_id;
  155. // $reciveInfo = $reciveModel->where($where)->find();
  156. // if(!$reciveInfo) {
  157. // $this->error(__('接单信息不存在!'));
  158. // }
  159. // if($reciveInfo["status"] == 2) {
  160. // $this->error(__('订单已关闭!'));
  161. // }
  162. // // 判断用户余额
  163. // $userModel = new \app\common\model\User();
  164. // $where = [];
  165. // $where["id"] = $this->auth->id;
  166. // $jewel = $userModel->where($where)->value("jewel");
  167. // $price = 0;
  168. // $reciveInfo["price"] >= 0 || $price = 0;
  169. // $reciveInfo["price"] >= 0 && $price = $reciveInfo["price"];
  170. // if($price * $num >= $jewel) {
  171. // $this->error(__('钻石余额不足,请先充值!'));
  172. // }
  173. // // 添加订单信息
  174. // $orderModel = new \app\common\model\DispatchOrder();
  175. // $data = [];
  176. // $data["user_id"] = $this->auth->id;
  177. // $data["recive_id"] = $recive_id;
  178. // $data["price"] = $reciveInfo["price"];
  179. // $data["num"] = $num;
  180. // $data["describe"] = $describe;
  181. // $data["createtime"] = time();
  182. // $res = $orderModel->insertGetId($data);
  183. // if($res) {
  184. // $this->success('订单创建成功!');
  185. // } else {
  186. // $this->error(__('订单创建失败!'));
  187. // }
  188. //
  189. // }
  190. // /**
  191. // * 获取接单信息
  192. // */
  193. // public function getReciveInfo() {
  194. // $recive_id = $this->request->request('recive_id',0,"intval"); // 接单ID
  195. // if (!$recive_id) {
  196. // $this->error(__('Invalid parameters'));
  197. // }
  198. // // 获取接单信息
  199. // $reciveModel = new \app\common\model\DispatchRecive();
  200. // $where = [];
  201. // $where["r.id"] = $recive_id;
  202. // $reciveInfo = $reciveModel->alias("r")
  203. // ->field("r.id,u.id as user_id,r.back_image,a.voice,u.avatar,u.nickname,s.name as skill_name,r.price,s.unit,r.service_time,r.describe,r.tags")
  204. // ->join("hx_user u","u.id = r.user_id")
  205. // ->join("hx_dispatch_skill s","s.id = r.skill_id")
  206. // ->join("hx_dispatch_auth a","a.skill_id = s.id")
  207. // ->where($where)
  208. // ->find();
  209. // if(!$reciveInfo) {
  210. // $this->error(__('数据为空!'));
  211. // }
  212. // // 在线状态和ID
  213. // $reciveInfo["is_online"] = 1;
  214. // $reciveInfo["line_id"] = 234224;
  215. // // 已服务人数
  216. // $where = [];
  217. // $where["user_id"] = $reciveInfo["user_id"];
  218. // $where["status"] = 2;
  219. // $reciveInfo["service_num"] = \app\common\model\DispatchOrder::where(["user_id"])->count();
  220. // // 主播认证通过的所有技能标签
  221. // $authModel = new \app\common\model\DispatchAuth();
  222. // $where = [];
  223. // $where["a.user_id"] = $reciveInfo["user_id"];
  224. // $where["a.status"] = $reciveInfo["user_id"];
  225. // $skillArr = $authModel->alias("a")
  226. // ->field("s.id,s.name")
  227. // ->join("hx_dispatch_skill s","s.id = a.skill_id")
  228. // ->where(["user_id"])->select();
  229. //
  230. // $reciveInfo["skills"] = $skillArr;
  231. //
  232. // $this->success('获取成功!',$reciveInfo);
  233. // }
  234. /**
  235. * 获取接单列表
  236. */
  237. public function getReciveList() {
  238. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  239. $is_recommend = $this->request->request('is_recommend'); // 是否推荐:1=是,0=否
  240. $gender = $this->request->request('gender',-1); // 性别:1=男,0=女,-1=全部
  241. $page = $this->request->request('page',1); // 分页
  242. $pageNum = $this->request->request('pageNum',10); // 分页
  243. // 分页搜索构建
  244. $pageStart = ($page-1)*$pageNum;
  245. if(!in_array($gender,[-1,0,1])) {
  246. $gender = -1;
  247. }
  248. $authModel = new \app\common\model\DispatchAuth();
  249. $where = [];
  250. if ($skill_id > 0) {
  251. $where["a.skill_id"] = $skill_id;
  252. } else if($is_recommend == 1) {
  253. $where["a.is_recommend"] = $is_recommend;
  254. $where["a.is_main"] = 1;
  255. }
  256. $where["a.status"] = 1;
  257. $gender == -1 || $where["u.gender"] = $gender;
  258. $authList = $authModel->alias("a")
  259. ->field("a.id,u.avatar,ds.name as skill_name,a.voice,a.voice_time,a.price,ds.unit,u.nickname,u.gender")
  260. ->join("hx_user u","u.id = a.user_id")
  261. ->join("hx_dispatch_skill ds","ds.id = a.skill_id")
  262. ->limit($pageStart,$pageNum)
  263. ->where($where)
  264. ->select();
  265. $this->success('获取成功!',$authList);
  266. }
  267. /**
  268. * 获取推荐列表
  269. */
  270. public function getReciveCommentList() {
  271. $page = $this->request->request('page',1); // 分页
  272. $pageNum = $this->request->request('pageNum',10); // 分页
  273. // 分页搜索构建
  274. $pageStart = ($page-1)*$pageNum;
  275. $authModel = new \app\common\model\DispatchAuth();
  276. $where = [];
  277. $where["a.is_main"] = 1;
  278. $where["a.status"] = 1;
  279. $where["a.is_recommend"] = 1;
  280. $authList = $authModel->alias("a")
  281. ->field("a.id,u.avatar,ds.name as skill_name,a.voice,a.voice_time,a.price,ds.unit,u.nickname,u.gender")
  282. ->join("hx_user u","u.id = a.user_id")
  283. ->join("hx_dispatch_skill ds","ds.id = a.skill_id")
  284. ->limit($pageStart,$pageNum)
  285. ->where($where)
  286. ->select();
  287. $this->success('获取成功!',$authList);
  288. }
  289. /**
  290. * 获取接单信息
  291. */
  292. public function getReciveInfo() {
  293. $auth_id = $this->request->request('auth_id',0,"intval"); // 技能认证ID
  294. if (!$auth_id) {
  295. $this->error(__('Invalid parameters'));
  296. }
  297. // 获取接单信息
  298. $authModel = new \app\common\model\DispatchAuth();
  299. $where = [];
  300. $where["a.id"] = $auth_id;
  301. $authInfo = $authModel->alias("a")
  302. ->field("a.id,a.user_id,a.image,a.voice,a.voice_time,u.avatar,u.nickname,u.u_id,u.is_online,s.id as skill_id,s.name as skill_name,s.image as skill_icon, a.price,s.unit,a.remarks")
  303. ->join("hx_user u","u.id = a.user_id")
  304. ->join("hx_dispatch_skill s","s.id = a.skill_id")
  305. ->where($where)
  306. ->find();
  307. if(!$authInfo) {
  308. $this->error(__('数据为空!'));
  309. }
  310. // 已服务人数
  311. $where = [];
  312. $where["recive_id"] = $authInfo["user_id"];
  313. $where["status"] = 3;
  314. $authInfo["service_num"] = \app\common\model\DispatchOrder::where($where)->count();
  315. // 主播认证通过的所有技能标签
  316. $viewuserskillModel = new \app\common\model\ViewUserSkill();
  317. $skillArr = $viewuserskillModel->getSkillInfoId($authInfo["user_id"]);
  318. // 查看者是否已关注
  319. $followid = \app\common\model\UserFansFollow::where(["fans_id"=>$this->auth->id,"user_id"=>$authInfo["user_id"]])->value("id");
  320. $authInfo["is_follow"] = $followid>0?1:0;
  321. $authInfo["skills"] = $skillArr;
  322. $this->success('获取成功!',$authInfo);
  323. }
  324. /**
  325. * 创建订单
  326. */
  327. public function addOrder() {
  328. $user_id = $this->request->request('user_id',0,"intval"); // 主播用户ID
  329. $skill_id = $this->request->request('skill_id',0,"intval"); // 技能ID
  330. $num = $this->request->request('num',0,"intval"); // 数量
  331. $make_time = $this->request->request('make_time'); // 预约时间 格式:时间戳。
  332. $describe = $this->request->request('describe'); // 备注要求
  333. $makeTimeLen = strlen($make_time);
  334. if (!empty($make_time) && $makeTimeLen > 10) {//处理时间戳位数问题
  335. $make_time = substr($make_time,0,10);
  336. }
  337. if (!$skill_id || !$num) {
  338. $this->error(__('Invalid parameters'));
  339. }
  340. $orderModel = new \app\common\model\DispatchOrder();
  341. // 获取接单信息
  342. $authModel = new \app\common\model\DispatchAuth();
  343. $where = [];
  344. $where["user_id"] = $user_id;
  345. $where["skill_id"] = $skill_id;
  346. $where["status"] = 1;
  347. $authInfo = $authModel->where($where)->find();
  348. if(!$authInfo) {
  349. $this->error(__('技能认证信息不存在,或认证未通过!'),[],103);
  350. }
  351. // 获取技能信息
  352. $skillModel = new \app\common\model\DispatchSkill();
  353. $where = [];
  354. $where["id"] = $skill_id;
  355. $skillInfo = $skillModel->where($where)->find();
  356. if(!$skillInfo) {
  357. $this->error(__('技能信息未找到,请稍后重试!'));
  358. }
  359. // 判断用户余额
  360. $userModel = new \app\common\model\User();
  361. $where = [];
  362. $where["id"] = $this->auth->id;
  363. $jewel = $userModel->where($where)->value("jewel");
  364. $price = 0;
  365. $authInfo["price"] >= 0 || $price = 0;
  366. $authInfo["price"] >= 0 && $price = $authInfo["price"];
  367. $money = $price * $num;
  368. if($money > $jewel) {
  369. $this->error(__('钻石余额不足,请先充值!'),[],100);
  370. }
  371. // 禁止自己下单
  372. if($this->auth->id == $authInfo["user_id"]) {
  373. $this->error(__('禁止自己下单!'),[],101);
  374. }
  375. // 查看当前用户是否有未支付订单
  376. $where = [];
  377. $where["user_id"] = $this->auth->id;
  378. $where["status"] = 0;
  379. $orderInfo = $orderModel->where($where)->find();
  380. // $orderInfo && $this->error(__('您有未支付订单,请先支付!'),[],102);
  381. Db::startTrans();
  382. try{
  383. // 添加订单信息
  384. $out_trade_no = date("YmdHis").rand(100000,999999);// 产生订单号
  385. $data = [];
  386. $data["user_id"] = $this->auth->id;
  387. $data["order_no"] = $out_trade_no;
  388. $data["recive_id"] = $authInfo["user_id"];
  389. $data["auth_id"] = $authInfo["id"];
  390. $data["price"] = $authInfo["price"];
  391. $data["num"] = $num;
  392. $data["skill_image"] = $skillInfo["image"];
  393. $data["skill_name"] = $skillInfo["name"];
  394. $data["skill_unit"] = $skillInfo["unit"];
  395. $data["make_time"] = $make_time;
  396. $data["describe"] = $describe;
  397. $data["status"] = 0;
  398. $data["createtime"] = time();
  399. $res = $orderModel->insertGetId($data);
  400. if($res) {
  401. Db::commit();
  402. $this->success('订单创建成功!',["order_no"=>$out_trade_no]);
  403. } else {
  404. $this->error(__('订单创建失败!'));
  405. }
  406. }catch (ValidateException $e) {
  407. Db::rollback();
  408. $this->error($e->getMessage());
  409. } catch (PDOException $e) {
  410. Db::rollback();
  411. $this->error($e->getMessage());
  412. } catch (Exception $e) {
  413. Db::rollback();
  414. $this->error($e->getMessage());
  415. }
  416. }
  417. /**
  418. * 添加订单评价内容
  419. */
  420. public function addOrderComment() {
  421. $order_no = $this->request->request('order_no'); // 订单编号
  422. $content = $this->request->request('content'); // 评价内容
  423. if (!$order_no && !$content) {
  424. $this->error(__('Invalid parameters'));
  425. }
  426. Db::startTrans();
  427. try{
  428. $ordercommentModel = new \app\common\model\DispatchOrderComment();
  429. $orderModel = new \app\common\model\DispatchOrder();
  430. $orderInfo = $orderModel->where(["order_no"=>$order_no])->find();
  431. if(!$orderInfo) $this->error(__('未查询到订单信息!'));
  432. if($orderInfo["status"] != 3) $this->error(__('当前订单状态不允许评价!'));
  433. $data= [];
  434. $data["user_id"] = $this->auth->id;
  435. $data["order_id"] = $orderInfo["id"];
  436. $data["content"] = $content;
  437. $data["createtime"] = time();
  438. $res1 = $ordercommentModel->insert($data);
  439. $res2 = $orderModel->update(["is_comment"=>1],["order_no"=>$order_no]);
  440. if($res1 && $res2) {
  441. Db::commit();
  442. $this->success('评价成功!');
  443. } else {
  444. $this->error(__('订单创建失败!'));
  445. }
  446. }catch (ValidateException $e) {
  447. Db::rollback();
  448. $this->error($e->getMessage());
  449. } catch (PDOException $e) {
  450. Db::rollback();
  451. $this->error($e->getMessage());
  452. } catch (Exception $e) {
  453. Db::rollback();
  454. $this->error($e->getMessage());
  455. }
  456. }
  457. /**
  458. * 钻石余额支付订单
  459. */
  460. public function jewelPay() {
  461. $order_no = $this->request->request('order_no'); // 订单号
  462. if (!$order_no) {
  463. $this->error(__('Invalid parameters'));
  464. }
  465. // 获取订单信息
  466. $orderModel = new \app\common\model\DispatchOrder();
  467. $where = [];
  468. $where["order_no"] = $order_no;
  469. $orderInfo = $orderModel->where($where)->find();
  470. if(!$orderInfo) {
  471. $this->error(__('订单信息获取失败!'));
  472. }
  473. if($orderInfo["status"] != 0) {
  474. $this->error(__('订单状态有误!'),[],104);
  475. }
  476. // 判断用户余额
  477. $userModel = new \app\common\model\User();
  478. $where = [];
  479. $where["id"] = $this->auth->id;
  480. $jewel = $userModel->where($where)->value("jewel");
  481. $money = $orderInfo["price"] * $orderInfo["num"];
  482. if($money > $jewel) {
  483. $this->error(__('钻石余额不足,请先充值!'),[],100);
  484. }
  485. // 去支付
  486. Db::startTrans();
  487. try{
  488. // 扣除用户余额
  489. $where = [];
  490. $where["id"] = $this->auth->id;
  491. $userInfo = $userModel->where($where)->find();
  492. $res1 = $userModel->where($where)->setDec("jewel",$money);
  493. $res2 = $userModel->where($where)->setInc("frozen",$money);
  494. // 添加用户余额变动记录
  495. $userjewellogModel = new \app\common\model\UserJewelLog();
  496. $data = [];
  497. $data["user_id"] = $this->auth->id;
  498. $data["value"] = $money;
  499. $data["mode"] = "-";
  500. $data["before"] = $userInfo["jewel"];
  501. $data["balance"] = $userInfo["jewel"]-$money;
  502. $data["detail"] = "下单扣除余额";
  503. $data["createtime"] = time();
  504. $res3 = $userjewellogModel->insertGetId($data);
  505. // 添加订单信息
  506. $orderModel = new \app\common\model\DispatchOrder();
  507. // 修改订单状态
  508. $where = [];
  509. $where["order_no"] = $order_no;
  510. $data = [];
  511. $data["status"] = 1;
  512. $res4 = $orderModel->update($data,$where);
  513. if($res1 && $res2 && $res3 && $res4) {
  514. Db::commit();
  515. // 短信通知
  516. $reciveuserInfo = $userModel->where(["id"=>$orderInfo["recive_id"]])->find();
  517. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"有人接单啦",'orderNotice');
  518. // 系统消息通知
  519. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的派单,技能:".$orderInfo["skill_name"]."有人接单啦,请登录伴声app查看!");
  520. $this->success('支付成功!');
  521. } else {
  522. $this->error(__('订单创建失败!'));
  523. }
  524. }catch (ValidateException $e) {
  525. Db::rollback();
  526. $this->error($e->getMessage());
  527. } catch (PDOException $e) {
  528. Db::rollback();
  529. $this->error($e->getMessage());
  530. } catch (Exception $e) {
  531. Db::rollback();
  532. $this->error($e->getMessage());
  533. }
  534. }
  535. /**
  536. * 更新订单状态
  537. */
  538. public function changeOrder() {
  539. $order_no = $this->request->request('order_no'); // 订单号
  540. $status = $this->request->request('status'); // 状态:-2=已取消,-1=拒绝接单,2=进行中,3=已完成
  541. if (!$order_no || !$status) {
  542. $this->error(__('Invalid parameters'));
  543. }
  544. if(!in_array($status,["-2","-1","2","3"])) {
  545. $this->error("非法的订单状态参数");
  546. }
  547. // 获取订单信息
  548. $orderModel = new \app\common\model\DispatchOrder();
  549. $where = [];
  550. $where["order_no"] = $order_no;
  551. $orderInfo = $orderModel->where($where)->find();
  552. if(!$orderInfo) {
  553. $this->error(__('订单信息获取失败!'));
  554. }
  555. if($orderInfo["status"] == $status) {
  556. $this->error(__('当前订单状态无变更要求!'),[],104);
  557. }
  558. // 验证用户权限
  559. if($status == -1 && $orderInfo["recive_id"] != $this->auth->id) {
  560. $this->error(__('无权限操作!'),[],103);
  561. }
  562. // 验证用户权限
  563. if(($status == -2 || $status == 3) && $orderInfo["user_id"] != $this->auth->id) {
  564. $this->error(__('无权限操作!'),[],103);
  565. }
  566. if($status == -1) { // 拒绝订单
  567. if($orderInfo["status"] != 1) {
  568. $this->error(__('当前订单状态不支持拒绝订单!'),[],104);
  569. }
  570. // 完成订单操作需要更新余额记录
  571. Db::startTrans();
  572. try{
  573. $userModel = new \app\common\model\User();
  574. $money = $orderInfo["price"] * $orderInfo["num"];
  575. // 解冻用户余额
  576. $where = [];
  577. $where["id"] = $this->auth->id;
  578. $userInfo = $userModel->where($where)->find();
  579. if($userInfo["frozen"] - $money < 0) {
  580. $this->error(__('账户资金异常,请联系管理员!'),[],105);
  581. }
  582. $res1 = $userModel->where($where)->setDec("frozen",$money);
  583. $res2 = $userModel->where($where)->setInc("jewel",$money);
  584. // 添加用户余额变动记录
  585. $userjewellogModel = new \app\common\model\UserJewelLog();
  586. $res3 = $userjewellogModel->addUserJewelLog($orderInfo["recive_id"], $money, "+", $userInfo["jewel"], "拒绝订单返还余额", 7);
  587. // 更新订单状态
  588. $data = [];
  589. $data["status"] = $status;
  590. $where = [];
  591. $where["order_no"] = $order_no;
  592. $res4 = $orderModel->update($data,$where);
  593. if($res1 && $res2 && $res3 && $res4) {
  594. Db::commit();
  595. // 短信通知
  596. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["user_id"]])->find();
  597. //Smslib::notice($reciveuserInfo->mobile, $orderInfo["skill_name"],"被拒绝接单",'orderNotice');
  598. // 系统消息通知
  599. \app\common\model\Message::addMessage($orderInfo["user_id"],"派单通知","您的订单,技能:".$orderInfo["skill_name"]."已被拒绝接单,请登录伴声app查看!");
  600. $this->success('订单完成。');
  601. } else {
  602. $this->error(__('订单更新失败!请稍后重试'));
  603. }
  604. }catch (ValidateException $e) {
  605. Db::rollback();
  606. $this->error($e->getMessage());
  607. } catch (PDOException $e) {
  608. Db::rollback();
  609. $this->error($e->getMessage());
  610. } catch (Exception $e) {
  611. Db::rollback();
  612. $this->error($e->getMessage());
  613. }
  614. } elseif($status == 3) { // 订单完成
  615. if($orderInfo["status"] != 2) {
  616. $this->error(__('当前订单状态不支持直接变更为已完成!'),[],104);
  617. }
  618. // 完成订单操作需要更新余额记录
  619. Db::startTrans();
  620. try{
  621. $userModel = new \app\common\model\User();
  622. $money = $orderInfo["price"] * $orderInfo["num"];
  623. // 增加用户余额
  624. $where = [];
  625. $where["id"] = $orderInfo["recive_id"];
  626. $userInfo = $userModel->where($where)->find();
  627. //$res1 = $userModel->where($where)->setInc("jewel",$money);
  628. $jewelMoneyRate = config('site.money_to_jewel');//1人民币兑换钻石数
  629. $moneyRmb = bcdiv($money,$jewelMoneyRate);
  630. $res1 = $userModel->where($where)->setInc("money",$moneyRmb);
  631. $where = [];
  632. $where["id"] = $this->auth->id;
  633. $res2 = $userModel->where($where)->setDec("frozen",$money);
  634. // 添加用户余额变动记录
  635. /*$userjewellogModel = new \app\common\model\UserJewelLog();
  636. $res3 = $userjewellogModel->addUserJewelLog($orderInfo["recive_id"], $money, "+", $userInfo["jewel"], "完成用户订单获得收益", 8);*/
  637. $remark = $orderInfo['skill_name'].'服务';
  638. $rs_wallet = model('wallet')->lockChangeAccountRemain($orderInfo["recive_id"],$moneyRmb,'+',$userInfo['money'],$remark,106,'money');
  639. $res3 = false;
  640. if($rs_wallet['status'] == false){
  641. $this->error($rs_wallet['msg']);
  642. Db::rollback();
  643. } else {
  644. $res3 = true;
  645. }
  646. // 更新订单状态
  647. $data = [];
  648. $data["status"] = $status;
  649. $where = [];
  650. $where["order_no"] = $order_no;
  651. $res4 = $orderModel->update($data,$where);
  652. if($res1 && $res2 && $res3 && $res4) {
  653. Db::commit();
  654. // 短信通知
  655. //$reciveuserInfo = $userModel->where(["id"=>$orderInfo["recive_id"]])->find();
  656. //Smslib::send($reciveuserInfo->mobile, $orderInfo["skill_name"], "顺利完成啦!",'orderNotice');
  657. // 系统消息通知
  658. \app\common\model\Message::addMessage($orderInfo["recive_id"],"派单通知","您的派单,技能:".$orderInfo["skill_name"]." 完成啦!,请登录GG语音app查看!");
  659. $this->success('恭喜!订单完成。');
  660. } else {
  661. $this->error(__('订单更新失败!请稍后重试'));
  662. }
  663. }catch (ValidateException $e) {
  664. Db::rollback();
  665. $this->error($e->getMessage());
  666. } catch (PDOException $e) {
  667. Db::rollback();
  668. $this->error($e->getMessage());
  669. } catch (Exception $e) {
  670. Db::rollback();
  671. $this->error($e->getMessage());
  672. }
  673. } else {
  674. $data = [];
  675. $data["status"] = $status;
  676. $where = [];
  677. $where["order_no"] = $order_no;
  678. $res = $orderModel->update($data,$where);
  679. if($res) {
  680. $this->success('更新成功!');
  681. } else {
  682. $this->error(__('订单创建失败!'));
  683. }
  684. }
  685. }
  686. /**
  687. * 获取技能列表
  688. */
  689. public function getSkillList() {
  690. $skillModel = new \app\common\model\DispatchSkill();
  691. $skillList = $skillModel->select();
  692. if ($skillList) {
  693. $this->success(__('获取成功!'), $skillList);
  694. } else {
  695. $this->success(__('数据为空!'));
  696. }
  697. }
  698. /**
  699. * 获取技能等级列表
  700. */
  701. public function getSkillLevelList() {
  702. $skill_id = $this->request->request('skill_id'); // 技能ID
  703. if (!$skill_id) {
  704. $this->error(__('Invalid parameters'));
  705. }
  706. $skilllevelModel = new \app\common\model\DispatchSkillLeaver();
  707. $where = [];
  708. $where["skill_id"] = $skill_id;
  709. $skilllevelList = $skilllevelModel->where($where)->select();
  710. if ($skilllevelList) {
  711. $this->success(__('获取成功!'), $skilllevelList);
  712. } else {
  713. $this->success(__('数据为空!'));
  714. }
  715. }
  716. /**
  717. * 获取技能区域列表
  718. */
  719. public function getSkillAreaList() {
  720. $skill_id = $this->request->request('skill_id'); // 技能ID
  721. if (!$skill_id) {
  722. $this->error(__('Invalid parameters'));
  723. }
  724. $skillareaModel = new \app\common\model\DispatchSkillArea();
  725. $where = [];
  726. $where["skill_id"] = $skill_id;
  727. $skillareaList = $skillareaModel->where($where)->select();
  728. if ($skillareaList) {
  729. $this->success(__('获取成功!'), $skillareaList);
  730. } else {
  731. $this->success(__('数据为空!'));
  732. }
  733. }
  734. /**
  735. * 获取订单列表
  736. */
  737. public function getOrderList() {
  738. $type = $this->request->request('type',1); // 订单类型:1=我的下单,2=我的接单
  739. $status = $this->request->request('status',999); // 订单状态:999=全部,-2=已取消,-1=拒绝接单,0=待付款,1=待确定,2=进行中,3=已完成
  740. $page = $this->request->request('page',1); // 分页
  741. $pageNum = $this->request->request('pageNum',10); // 分页
  742. // 分页搜索构建
  743. $pageStart = ($page-1)*$pageNum;
  744. $user = "o.recive_id";
  745. $type == 1 && $user = "o.recive_id";
  746. $type == 2 && $user = "o.user_id";
  747. // 主体列表信息
  748. $orderModel = new \app\common\model\DispatchOrder();
  749. $where = [];
  750. $type == 1 && $where["o.user_id"] = $this->auth->id;
  751. $type == 2 && $where["o.recive_id"] = $this->auth->id;
  752. $status != 999 && $where["o.status"] = $status;
  753. $orderList = $orderModel->alias("o")
  754. ->field("o.id,".$user." as user_id,o.make_time,o.describe,o.order_no,u.avatar,u.nickname,o.skill_image,o.skill_name,o.price,o.skill_unit,o.num,o.status,o.is_comment,o.createtime")
  755. ->join("hx_user u","u.id = ".$user)
  756. ->where($where)
  757. ->order("createtime","desc")
  758. ->limit($pageStart,$pageNum)
  759. ->select();
  760. if(!$orderList) {
  761. $this->success(__('数据为空!'),[]);
  762. }
  763. foreach($orderList as $k => &$v) {
  764. $v["make_time"] = date("Y-m-d H:i",$v["make_time"]);
  765. $v["createtime"] = date("Y-m-d",$v["createtime"]);
  766. $v["status_text"] = $orderModel->getStateAttr($v["status"]);
  767. }
  768. $this->success(__('获取成功!'), $orderList);
  769. }
  770. }