Account.php 14 KB

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