Wxapp.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\admin\model;
  10. use app\common\model\Wxapp as WxappModel;
  11. use app\admin\model\store\User as StoreUser;
  12. /**
  13. * 微信小程序模型
  14. * Class Wxapp
  15. * @package app\admin\model
  16. */
  17. class Wxapp extends WxappModel
  18. {
  19. /**
  20. * 获取小程序列表
  21. * @param boolean $is_recycle
  22. * @return \think\Paginator
  23. * @throws \think\exception\DbException
  24. */
  25. public function getList($is_recycle = false)
  26. {
  27. return $this->where('is_recycle', '=', (int)$is_recycle)
  28. ->where('is_delete', '=', 0)
  29. ->order(['create_time' => 'desc'])
  30. ->paginate(15, false, [
  31. 'query' => request()->request()
  32. ]);
  33. }
  34. /**
  35. * 从缓存中获取商城名称
  36. * @param $data
  37. * @return array
  38. */
  39. public function getStoreName($data)
  40. {
  41. $names = [];
  42. foreach ($data as $wxapp) {
  43. $names[$wxapp['wxapp_id']] = Setting::getItem('store', $wxapp['wxapp_id'])['name'];
  44. }
  45. return $names;
  46. }
  47. /**
  48. * 新增记录
  49. * @param $data
  50. * @return bool
  51. * @throws \think\exception\PDOException
  52. */
  53. public function add($data)
  54. {
  55. if ($data['password'] !== $data['password_confirm']) {
  56. $this->error = '确认密码不正确';
  57. return false;
  58. }
  59. $this->startTrans();
  60. try {
  61. // 添加小程序记录
  62. $this->allowField(true)->save($data);
  63. // 商城默认设置
  64. (new Setting)->insertDefault($this['wxapp_id'], $data['store_name']);
  65. // 新增商家用户信息
  66. $StoreUser = new StoreUser;
  67. if (!$StoreUser->add($this['wxapp_id'], $data)) {
  68. $this->error = $StoreUser->error;
  69. return false;
  70. }
  71. // 新增小程序默认帮助
  72. (new WxappHelp)->insertDefault($this['wxapp_id']);
  73. // 新增小程序diy配置
  74. (new WxappPage)->insertDefault($this['wxapp_id']);
  75. // 新增小程序分类页模板
  76. (new WxappCategory)->insertDefault($this['wxapp_id']);
  77. $this->commit();
  78. return true;
  79. } catch (\Exception $e) {
  80. $this->error = $e->getMessage();
  81. $this->rollback();
  82. return false;
  83. }
  84. }
  85. /**
  86. * 移入移出回收站
  87. * @param bool $is_recycle
  88. * @return false|int
  89. */
  90. public function recycle($is_recycle = true)
  91. {
  92. return $this->save(['is_recycle' => (int)$is_recycle]);
  93. }
  94. /**
  95. * 软删除
  96. * @return false|int
  97. */
  98. public function setDelete()
  99. {
  100. return $this->save(['is_delete' => 1]);
  101. }
  102. }