Taxi.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace app\agent\controller\order;
  3. use app\common\controller\AgentController;
  4. use app\http\IResponse;
  5. use EasyWeChat\Factory;
  6. use Lettered\Support\Auth as IAuth;
  7. use think\App;
  8. use think\Exception;
  9. use think\Request;
  10. class Taxi extends AgentController
  11. {
  12. protected $model;
  13. protected $wechat;
  14. /**
  15. * Skill constructor.
  16. * @param App|null $app
  17. * @param IAuth $auth
  18. */
  19. public function __construct(App $app = null, IAuth $auth)
  20. {
  21. parent::__construct($app, $auth);
  22. $this->model = new \app\agent\model\order\Taxi();
  23. // 加载配置
  24. $wechat = sys_config('','wechat');
  25. $this->wechat = Factory::miniProgram([
  26. 'app_id' => $wechat['mini_appid'],
  27. 'secret' => $wechat['mni_secret_key'],
  28. 'response_type' => 'array',
  29. 'log' => [
  30. 'level' => 'debug',
  31. 'file' => app()->getRuntimePath() . 'log/'.date('Ym').'/wechat_debug.log',
  32. ],
  33. ]);
  34. }
  35. /**
  36. * 显示资源列表
  37. *
  38. * @return \think\Response
  39. * @throws \Lettered\Support\Exceptions\FailedException
  40. * @throws \think\exception\DbException
  41. */
  42. public function index()
  43. {
  44. $where[] = ['area_id', 'eq', $this->auth->user()['area_id']];
  45. $list = $this->model->where($where)->with(['taxi','taxi.user','user','category'])
  46. ->order('created_at' , 'desc')
  47. ->paginate(input('limit'),false);
  48. return IResponse::paginate($list);
  49. }
  50. /**
  51. * 显示创建资源表单页.
  52. *
  53. * @return \think\Response
  54. */
  55. public function create()
  56. {
  57. //
  58. }
  59. /**
  60. * 保存新建的资源
  61. *
  62. * @param \think\Request $request
  63. * @return \think\Response
  64. */
  65. public function save(Request $request)
  66. {
  67. //
  68. }
  69. /**
  70. * 显示指定的资源
  71. *
  72. * @param int $id
  73. * @return \think\Response
  74. */
  75. public function read($id)
  76. {
  77. //
  78. }
  79. /**
  80. * 显示编辑资源表单页.
  81. *
  82. * @param int $id
  83. * @return \think\Response
  84. */
  85. public function edit($id)
  86. {
  87. //
  88. }
  89. /**
  90. * 保存更新的资源
  91. *
  92. * @param \think\Request $request
  93. * @param int $id
  94. * @return \think\Response
  95. */
  96. public function update(Request $request, $id)
  97. {
  98. //
  99. }
  100. /**
  101. * @desc 指派司机
  102. * @param $id
  103. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  104. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  105. * @throws \GuzzleHttp\Exception\GuzzleException
  106. * @throws \think\exception\PDOException
  107. * @author weichuanbao<654745815@qq.com>
  108. * @date 2021/12/27 0027
  109. */
  110. public function dispense($id)
  111. {
  112. $model = model('common/TaxiOrder');
  113. $order = $model->findBy($id);
  114. //TODO 应该把当前车俩设置为占用
  115. // 获取用户信息
  116. $user = model('common/Users')->findBy($order['user_id']);
  117. $Taxi = new \app\common\model\Taxi();
  118. $driver = $Taxi->with('user')->find($order['taxi_id']);
  119. $result = false;
  120. $this->model->startTrans();
  121. try {
  122. // 订阅消息
  123. $this->wechat->subscribe_message->send([
  124. 'template_id' => 'v2nNL6LKvYr0pyGsTBw5r9Zaa7lS63Hm5AjvJq-tKWc', // 所需下发的订阅模板id
  125. 'touser' => $user['open_id'],//'o11PJ5Qs7asNf0KgdOaKjjWTeGpo', //
  126. 'page' => '/pages/motor/order/order',
  127. 'data' => [
  128. 'character_string1' => [
  129. 'value' => $order['order_no'],
  130. ],
  131. 'thing5' => [
  132. 'value' => '电话请保持通畅,师傅正在赶往路上,请稍候',
  133. ],
  134. 'thing11' => [
  135. 'value' => $driver['user']['uname'],
  136. ],
  137. 'phone_number6' => [
  138. 'value' => $driver['user']['mobile'],
  139. ],
  140. 'time7' => [
  141. 'value' => date("Y/m/d H:i:s"),
  142. ],
  143. ]
  144. ]);
  145. // 状态以及时间
  146. $result = $model->updateBy($id,[
  147. 'status' => 3
  148. ]);
  149. $this->model->commit();
  150. }
  151. catch(Exception $e) {
  152. $this->model->rollback();
  153. }
  154. if ($result) {
  155. return IResponse::success([],'成功');
  156. }
  157. return IResponse::failure('失败');
  158. }
  159. /**
  160. * @desc 结束服务
  161. * @param $id
  162. * @throws \think\exception\PDOException
  163. * @author weichuanbao<654745815@qq.com>
  164. * @date 2021/12/27 0027
  165. */
  166. public function complete($id)
  167. {
  168. $model = model('common/TaxiOrder');
  169. $order = $model->findBy($id);
  170. $result = false;
  171. $this->model->startTrans();
  172. try {
  173. //TODO 应该把当前车俩设置为 空闲
  174. // 已支付订单,标记为已经服务了
  175. $model->updateBy($id,[
  176. 'status' => 4,
  177. 'served' => ($order['status'] == 3) ? '1' : '0'
  178. ]);
  179. $result = true;
  180. $this->model->commit();
  181. }
  182. catch(Exception $e) {
  183. $this->model->rollback();
  184. }
  185. if ($result) {
  186. return IResponse::success([],'成功');
  187. }
  188. return IResponse::failure('失败');
  189. }
  190. /**
  191. * 删除指定资源
  192. *
  193. * @param int $id
  194. * @return \think\Response
  195. */
  196. public function delete($id)
  197. {
  198. model('common/TaxiOrder')->deleteBy($id);
  199. return IResponse::success([],'成功');
  200. }
  201. }