You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
9.8 KiB

4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * 微信自定义菜单
  7. * 1、自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。
  8. * 2、一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。
  9. * 3、创建自定义菜单后,菜单的刷新策略是,在用户进入公众号会话页或公众号profile页时,如果发现上一次拉取菜单的请求在5分钟以前,就会拉取一下菜单,如果菜单有更新,就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。
  10. */
  11. class WeixinMenu extends BaseModel
  12. {
  13. protected $table = 'weixin_menu';
  14. public $timestamps = false;
  15. protected $hidden = array();
  16. protected $guarded = array(); //$guarded包含你不想被赋值的字段数组。
  17. const IS_SHOW = 0;
  18. public function getDb()
  19. {
  20. return DB::table($this->table);
  21. }
  22. /**
  23. * 列表
  24. * @param array $where 查询条件
  25. * @param string $order 排序
  26. * @param string $field 字段
  27. * @param int $offset 偏移量
  28. * @param int $limit 取多少条
  29. * @return array
  30. */
  31. public function getList($where = array(), $order = '', $field = '*', $offset = 0, $limit = 15)
  32. {
  33. $model = $this->getDb();
  34. if($where){$model = $model->where($where);}
  35. $res['count'] = $model->count();
  36. $res['list'] = array();
  37. if($res['count'] > 0)
  38. {
  39. if($field){if(is_array($field)){$model = $model->select($field);}else{$model = $model->select(\DB::raw($field));}}
  40. if($order){$model = parent::getOrderByData($model, $order);}
  41. if($offset){}else{$offset = 0;}
  42. if($limit){}else{$limit = 15;}
  43. $res['list'] = $model->skip($offset)->take($limit)->get();
  44. }
  45. return $res;
  46. }
  47. /**
  48. * 分页,用于前端html输出
  49. * @param array $where 查询条件
  50. * @param string $order 排序
  51. * @param string $field 字段
  52. * @param int $limit 每页几条
  53. * @param int $page 当前第几页
  54. * @return array
  55. */
  56. public function getPaginate($where = array(), $order = '', $field = '*', $limit = 15)
  57. {
  58. $res = $this->getDb();
  59. if($where){$res = $res->where($where);}
  60. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  61. if($order){$res = parent::getOrderByData($res, $order);}
  62. if($limit){}else{$limit = 15;}
  63. return $res->paginate($limit);
  64. }
  65. /**
  66. * 查询全部
  67. * @param array $where 查询条件
  68. * @param string $order 排序
  69. * @param string $field 字段
  70. * @param int $limit 取多少条
  71. * @return array
  72. */
  73. public function getAll($where = array(), $order = '', $field = '*', $limit = '', $offset = '')
  74. {
  75. $res = $this->getDb();
  76. if($where){$res = $res->where($where);}
  77. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  78. if($order){$res = parent::getOrderByData($res, $order);}
  79. if($offset){$res = $res->skip($offset);}
  80. if($limit){$res = $res->take($limit);}
  81. $res = $res->get();
  82. return $res;
  83. }
  84. /**
  85. * 获取一条
  86. * @param array $where 条件
  87. * @param string $field 字段
  88. * @return array
  89. */
  90. public function getOne($where, $field = '*')
  91. {
  92. $res = $this->getDb();
  93. if($where){$res = $res->where($where);}
  94. if($field){if(is_array($field)){$res = $res->select($field);}else{$res = $res->select(\DB::raw($field));}}
  95. $res = $res->first();
  96. return $res;
  97. }
  98. /**
  99. * 添加
  100. * @param array $data 数据
  101. * @return int
  102. */
  103. public function add(array $data,$type = 0)
  104. {
  105. if($type==0)
  106. {
  107. // 新增单条数据并返回主键值
  108. return self::insertGetId(parent::filterTableColumn($data,$this->table));
  109. }
  110. elseif($type==1)
  111. {
  112. /**
  113. * 添加单条数据
  114. * $data = ['foo' => 'bar', 'bar' => 'foo'];
  115. * 添加多条数据
  116. * $data = [
  117. * ['foo' => 'bar', 'bar' => 'foo'],
  118. * ['foo' => 'bar1', 'bar' => 'foo1'],
  119. * ['foo' => 'bar2', 'bar' => 'foo2']
  120. * ];
  121. */
  122. return self::insert($data);
  123. }
  124. }
  125. /**
  126. * 修改
  127. * @param array $data 数据
  128. * @param array $where 条件
  129. * @return int
  130. */
  131. public function edit($data, $where = array())
  132. {
  133. $res = $this->getDb();
  134. return $res->where($where)->update(parent::filterTableColumn($data, $this->table));
  135. }
  136. /**
  137. * 删除
  138. * @param array $where 条件
  139. * @return bool
  140. */
  141. public function del($where)
  142. {
  143. $res = $this->getDb();
  144. $res = $res->where($where)->delete();
  145. return $res;
  146. }
  147. /*
  148. //获取列表
  149. public static function getList(array $param)
  150. {
  151. extract($param); //参数:limit,offset
  152. if(isset($is_show) && $is_show!=-1){$where['is_show'] = $is_show;} //-1表示获取所有
  153. $where['pid'] = 0;
  154. $list = self::where($where)->orderBy('listorder', 'asc')->get();
  155. if($list)
  156. {
  157. foreach($list as $k=>$v)
  158. {
  159. $res[] = $v;
  160. $child = self::where(array('pid'=>$list[$k]->id,'is_show'=>self::IS_SHOW))->orderBy('listorder', 'asc')->get();
  161. if($child)
  162. {
  163. foreach($child as $key=>$value)
  164. {
  165. $res[] = $value;
  166. }
  167. }
  168. }
  169. }
  170. else
  171. {
  172. return false;
  173. }
  174. return $res;
  175. }
  176. public static function getOne($where)
  177. {
  178. return self::where($where)->first();
  179. }
  180. public static function add(array $data)
  181. {
  182. if ($id = self::insertGetId($data))
  183. {
  184. return $id;
  185. }
  186. return false;
  187. }
  188. public static function modify($where, array $data)
  189. {
  190. if (self::where($where)->update($data) === false)
  191. {
  192. return false;
  193. }
  194. return true;
  195. }
  196. //删除一条记录
  197. public static function remove($id)
  198. {
  199. if (!self::whereIn('id', explode(',', $id))->delete())
  200. {
  201. return false;
  202. }
  203. return true;
  204. } */
  205. //获取微信自定义菜单
  206. public static function getWeixinMenuJson()
  207. {
  208. $where['pid'] = 0;
  209. $where['is_show'] = self::IS_SHOW;
  210. $list = self::where($where)->orderBy('listorder', 'asc')->get();
  211. $res='';
  212. if($list)
  213. {
  214. foreach($list as $k=>$v)
  215. {
  216. $child = self::where(array('pid'=>$list[$k]->id,'is_show'=>self::IS_SHOW))->orderBy('listorder', 'asc')->get();
  217. if($child)
  218. {
  219. $temp_child='';
  220. foreach($child as $key=>$value)
  221. {
  222. if($value->type == 'click')
  223. {
  224. $temp_child[] = array(
  225. 'type'=>$value->type,
  226. 'name'=>$value->name,
  227. 'key'=>$value->key
  228. );
  229. }
  230. elseif($value->type == 'view')
  231. {
  232. $temp_child[] = array(
  233. 'type'=>$value->type,
  234. 'name'=>$value->name,
  235. 'url'=>$value->key
  236. );
  237. }
  238. elseif($value->type == 'miniprogram')
  239. {
  240. $temp_child[] = array(
  241. 'type'=>$value->type,
  242. 'name'=>$value->name,
  243. 'url'=>$value->key,
  244. 'appid'=>$value->appid,
  245. 'pagepath'=>$value->pagepath
  246. );
  247. }
  248. }
  249. $res[] = array(
  250. 'name'=>$value->name,
  251. 'sub_button'=>$temp_child
  252. );
  253. }
  254. else
  255. {
  256. if($v->type == 'click')
  257. {
  258. $res[] = array(
  259. 'type'=>$v->type,
  260. 'name'=>$v->name,
  261. 'key'=>$v->key
  262. );
  263. }
  264. elseif($v->type == 'view')
  265. {
  266. $res[] = array(
  267. 'type'=>$v->type,
  268. 'name'=>$v->name,
  269. 'url'=>$v->key
  270. );
  271. }
  272. elseif($v->type == 'miniprogram')
  273. {
  274. $res[] = array(
  275. 'type'=>$v->type,
  276. 'name'=>$v->name,
  277. 'url'=>$v->key,
  278. 'appid'=>$v->appid,
  279. 'pagepath'=>$v->pagepath
  280. );
  281. }
  282. }
  283. }
  284. }
  285. return json_encode($res);
  286. }
  287. }