Account.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace app\abutment\logic;
  3. use app\abutment\model\SupplierUser as SupplierUserModel;
  4. use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
  5. use app\abutment\model\SupplierUser;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Config;
  9. use think\facade\Db;
  10. use think\helper\Str;
  11. //供应商账号管理
  12. class Account
  13. {
  14. //登录
  15. public static function login(array $param = [])
  16. {
  17. $db = new SupplierUserModel();
  18. $res = $db->where([
  19. 'is_del' => $db::$is_del_normal,
  20. 'mobile' => $param['mobile']
  21. ])
  22. ->findOrEmpty()
  23. ->toArray();
  24. if (empty($res)) return json_show(1004, '该账号不存在');
  25. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  26. $password = get_encryption_password($param['password'], $res['salt']);
  27. if ($password['password'] != $res['password']) return json_show(1004, '密码错误');
  28. //更新token
  29. $token = Str::random(40, -1);
  30. $expire_int = mt_rand(3600, 7200);
  31. $expire_time = date('Y-m-d H:i:s', time() + $expire_int);
  32. $rs = $db
  33. ->where($db->getPk(), $res[$db->getPk()])
  34. ->save(['token' => $token, 'expire_time' => $expire_time]);
  35. if (!$rs) return json_show(1005, '更新账号token信息失败');
  36. $info = [
  37. 'uid' => $res['uid'],
  38. 'nickname' => $res['nickname'],
  39. 'mobile' => $res['mobile'],
  40. 'email' => $res['email'],
  41. 'token' => $token,
  42. 'expire_time' => $expire_time,
  43. ];
  44. //过期时间指定一个具体的日期的时候,还没到这个日期缓存就过期了,指定一个秒数,可以符合预期,邪门儿
  45. Cache::set(Config::get('redis_key.user_info_token') . $token, json_encode($info), $expire_int);
  46. return json_show(0, '登录成功', ['token' => $token, 'expire_time' => $expire_time]);
  47. }
  48. //获取用户信息
  49. public static function getUserInfo()
  50. {
  51. $db = new SupplierUserModel();
  52. $res = $db->where([
  53. 'uid' => request()->user['uid'],
  54. 'is_del' => $db::$is_del_normal,
  55. ])->findOrEmpty()
  56. ->toArray();
  57. if (empty($res)) return json_show(1004, '该账号不存在');
  58. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  59. $list = SupplierRelationUserModel::field('id,supplierNo,supplierName')
  60. ->where(['is_del' => $db::$is_del_normal, 'uid' => $res['uid']])
  61. ->select()
  62. ->toArray();
  63. //获取这些供应商的状态
  64. $status = Db::name('supplier')
  65. ->where('is_del', 0)
  66. ->whereIn('code', array_column($list, 'supplierNo'))
  67. ->column('status', 'code');
  68. foreach ($list as &$value) {
  69. $value['status'] = $status[$value['supplierNo']];
  70. }
  71. $info = [
  72. 'uid' => $res['uid'],
  73. 'nickname' => $res['nickname'],
  74. 'mobile' => $res['mobile'],
  75. 'email' => $res['email'],
  76. 'token' => $res['token'],
  77. 'expire_time' => $res['expire_time'],
  78. 'supplier_list' => $list,
  79. ];
  80. return json_show(0, '获取用户信息成功', $info);
  81. }
  82. //获取供应商账号列表
  83. public static function getAccountList(array $param = [])
  84. {
  85. $db = new SupplierUserModel();
  86. $rs = $db
  87. ->where('is_del', $db::$is_del_normal);
  88. if ($param['keyword'] != '') $rs->whereLike('nickname|mobile', '%' . $param['keyword'] . '%');
  89. if ($param['status'] != '') $rs->where('status', $param['status']);
  90. if ($param['supplierNo'] != '') {
  91. $uids = SupplierRelationUserModel::where([
  92. 'is_del' => $db::$is_del_normal,
  93. 'supplierNo' => $param['supplierNo']
  94. ])->column('uid');
  95. $rs->whereIn('uid', $uids);
  96. }
  97. $count = $rs->count('uid');
  98. $list = $rs
  99. ->field('uid,nickname,mobile,email,status,addtime,creater')
  100. ->order('addtime', 'desc')
  101. ->page($param['page'], $param['size'])
  102. ->append(['supplier_list'])
  103. ->withAttr('supplier_list', function ($val, $data) use ($db) {
  104. return SupplierRelationUserModel::where([
  105. 'is_del' => $db::$is_del_normal,
  106. 'uid' => $data['uid']
  107. ])->field('supplierNo,supplierName,status')
  108. ->select()
  109. ->toArray();
  110. })
  111. ->select()
  112. ->toArray();
  113. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  114. }
  115. //修改供应商账号密码
  116. public static function changePassword(array $param = [])
  117. {
  118. $db = new SupplierUserModel();
  119. $res = $db
  120. ->where([
  121. 'uid' => $param['uid'],
  122. 'is_del' => $db::$is_del_normal,
  123. ])
  124. ->field('uid,status')
  125. ->findOrEmpty()
  126. ->toArray();
  127. if (empty($res)) return json_show(1004, '该账号不存在');
  128. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  129. //更新密码
  130. $password = get_encryption_password($param['password']);
  131. $rs = $db
  132. ->where('uid', $res['uid'])
  133. ->save(['password' => $password['password'], 'salt' => $password['salt']]);
  134. return $rs ? json_show(0, '修改密码成功') : json_show(1005, '修改密码失败');
  135. }
  136. //获取供应商账号账号信息
  137. public static function readAccount(array $param = [])
  138. {
  139. $res = SupplierUser::field('uid,nickname,mobile,email')
  140. ->where([
  141. 'uid' => $param['uid'],
  142. 'is_del' => SupplierUser::$is_del_normal,
  143. ])
  144. ->append(['supplier_list'])
  145. ->withAttr('supplier_list', function ($val, $data) {
  146. return SupplierRelationUserModel::field('id,supplierNo,supplierName,status,addtime')
  147. ->where([
  148. 'is_del' => SupplierUser::$is_del_normal,
  149. 'uid' => $data['uid']
  150. ])
  151. ->select()
  152. ->toArray();
  153. })
  154. ->findOrEmpty()
  155. ->toArray();
  156. return json_show(0, '获取账号信息成功', $res);
  157. }
  158. //添加供应商账号
  159. public static function addAccount(array $param = [], string $token = '')
  160. {
  161. $user = GetUserInfo($token);
  162. //查找供应商名称
  163. $supplierName = Db::name('supplier')
  164. ->whereIn('code', $param['supplierNo'])
  165. ->column('name', 'code');
  166. if (empty($supplierName)) return json_show(1004, '供应商不存在');
  167. Db::connect('mysql_sys')->startTrans();
  168. try {
  169. $db = new SupplierUserModel();
  170. $res = $db
  171. ->field('uid')
  172. ->where([
  173. 'is_del' => $db::$is_del_normal,
  174. 'mobile' => $param['mobile'],
  175. ])->findOrEmpty();
  176. if (!$res->isEmpty()) throw new Exception('该手机号已存在');
  177. //新增账号
  178. $password = get_encryption_password(Config::get('app.default_password'));
  179. $uid = $db->insertGetId([
  180. 'nickname' => $param['nickname'],//姓名
  181. 'mobile' => $param['mobile'],//手机号
  182. 'email' => $param['email'],//邮箱
  183. 'password' => $password['password'],//密码密文
  184. 'salt' => $password['salt'],//盐值
  185. 'status' => $db::$status_normal,
  186. 'is_del' => $db::$is_del_normal,
  187. 'createrid' => $user['data']['user_id'] ?? 0,
  188. 'creater' => $user['data']['nickname'] ?? '',
  189. ]);
  190. $insert_data = [];
  191. foreach ($param['supplierNo'] as $supplierNo) {
  192. $insert_data[] = [
  193. 'uid' => $uid,
  194. 'supplierNo' => $supplierNo,
  195. 'supplierName' => $supplierName[$supplierNo] ?? '',
  196. 'status' => $db::$status_normal,
  197. 'is_del' => $db::$is_del_normal,
  198. 'createrid' => $user['data']['user_id'] ?? 0,
  199. 'creater' => $user['data']['nickname'] ?? '',
  200. ];
  201. }
  202. if ($insert_data) Db::connect('mysql_sys')
  203. ->name('supplier_relation_user')
  204. ->insertAll($insert_data);
  205. Db::connect('mysql_sys')->commit();
  206. return json_show(0, '操作成功');
  207. } catch (Exception $exception) {
  208. Db::connect('mysql_sys')->rollback();
  209. return json_show(1005, '操作失败' . $exception->getMessage());
  210. }
  211. }
  212. //修改供应商账号
  213. public static function editAccount(array $param = [])
  214. {
  215. $user = GetUserInfo($param['token']);
  216. Db::connect('mysql_sys')->startTrans();
  217. try {
  218. $db = new SupplierUserModel();
  219. $res = $db
  220. ->field('uid,mobile')
  221. ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal])
  222. ->findOrEmpty()
  223. ->toArray();
  224. if (empty($res)) return json_show(1004, '该账号不存在');
  225. if ($res['mobile'] != $param['mobile']) {
  226. $temp = $db
  227. ->field('uid')
  228. ->where(['mobile' => $param['mobile'], 'is_del' => $db::$is_del_normal])
  229. ->where('uid', '<>', $param['uid'])
  230. ->findOrEmpty()
  231. ->isEmpty();
  232. if (!$temp) throw new Exception('要修改的手机号已存在');
  233. }
  234. $db
  235. ->where('uid', $param['uid'])
  236. ->strict(false)
  237. ->save($param);
  238. $relation_db = new SupplierRelationUserModel();
  239. $insert = $retain = [];
  240. foreach ($param['supplier_list'] as $supplier) {
  241. if (isset($supplier['id']) && $supplier['id'] != 0) $retain[] = $supplier['id'];
  242. else $insert[] = [
  243. 'uid' => $param['uid'],
  244. 'supplierNo' => $supplier['supplierNo'],
  245. 'supplierName' => $supplier['supplierName'],
  246. 'status' => $db::$status_normal,
  247. 'is_del' => $db::$is_del_normal,
  248. 'createrid' => $user['data']['user_id'] ?? 0,
  249. 'creater' => $user['data']['nickname'] ?? 0,
  250. ];
  251. }
  252. //除了保留id,其余删除
  253. if (!empty($retain)) $relation_db->where([
  254. 'is_del' => $db::$is_del_normal,
  255. 'uid' => $param['uid']
  256. ])->whereNotIn('id', $retain)->save(['is_del' => $db::$is_del_deleted]);
  257. if ($insert) $relation_db->saveAll($insert);
  258. Db::connect('mysql_sys')->commit();
  259. return json_show(0, '操作成功');
  260. } catch (Exception $exception) {
  261. Db::connect('mysql_sys')->rollback();
  262. return json_show(1005, '操作失败,' . $exception->getMessage());
  263. }
  264. }
  265. //修改供应商账号状态
  266. public static function statusAccount(array $param = [])
  267. {
  268. Db::connect('mysql_sys')->startTrans();
  269. try {
  270. $db = new SupplierUserModel();
  271. $res = $db
  272. ->field('uid,status')
  273. ->where([
  274. 'uid' => $param['uid'],
  275. 'is_del' => $db::$is_del_normal,
  276. ])->findOrEmpty();
  277. if ($res->isEmpty()) throw new Exception('该账号不存在');
  278. if ($res->status == $param['status']) throw new Exception('不能重复操作');
  279. $db->where([
  280. 'uid' => $param['uid'],
  281. 'is_del' => $db::$is_del_normal,
  282. ])->where('status', '<>', $param['status'])
  283. ->save(['status' => $param['status']]);
  284. SupplierRelationUserModel::where([
  285. 'uid' => $param['uid'],
  286. 'is_del' => $db::$is_del_normal,
  287. ])->where('status', '<>', $param['status'])
  288. ->save(['status' => $param['status']]);
  289. Db::connect('mysql_sys')->commit();
  290. return json_show(0, '操作成功');
  291. } catch (Exception $exception) {
  292. Db::connect('mysql_sys')->rollback();
  293. return json_show(1005, '操作失败,' . $exception->getMessage());
  294. }
  295. }
  296. //删除供应商账号
  297. public static function deleteAccount(int $uid = 0)
  298. {
  299. Db::connect('mysql_sys')->startTrans();
  300. try {
  301. $db = new SupplierUserModel();
  302. $res = $db
  303. ->field('uid')
  304. ->where([
  305. 'uid' => $uid,
  306. 'is_del' => $db::$is_del_normal,
  307. ])->findOrEmpty();
  308. if ($res->isEmpty()) throw new Exception('该账号不存在或已删除');
  309. $db->where('uid', $uid)
  310. ->where('is_del', $db::$is_del_normal)
  311. ->save(['is_del' => $db::$is_del_deleted]);
  312. SupplierRelationUserModel::where('uid', $uid)
  313. ->where('is_del', $db::$is_del_normal)
  314. ->save(['is_del' => $db::$is_del_deleted]);
  315. Db::connect('mysql_sys')->commit();
  316. return json_show(0, '删除成功');
  317. } catch (Exception $exception) {
  318. Db::connect('mysql_sys')->rollback();
  319. return json_show(1005, '删除失败,' . $exception->getMessage());
  320. }
  321. }
  322. }