Dispatch.php 36 KB

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