Wxapp.php 2.7 KB

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