Account.php 14 KB

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