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.

180 lines
5.5 KiB

  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\ReturnData;
  4. /**
  5. * 微信自定义菜单
  6. * 1、自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。
  7. * 2、一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替。
  8. * 3、创建自定义菜单后,菜单的刷新策略是,在用户进入公众号会话页或公众号profile页时,如果发现上一次拉取菜单的请求在5分钟以前,就会拉取一下菜单,如果菜单有更新,就会刷新客户端的菜单。测试时可以尝试取消关注公众账号后再次关注,则可以看到创建后的效果。
  9. */
  10. class WeixinMenu extends BaseModel
  11. {
  12. protected $table = 'weixin_menu';
  13. public $timestamps = false;
  14. /**
  15. * 不能被批量赋值的属性
  16. *
  17. * @var array
  18. */
  19. protected $guarded = array();
  20. const IS_SHOW = 0;
  21. //获取列表
  22. public static function getList(array $param)
  23. {
  24. extract($param); //参数:limit,offset
  25. if(isset($is_show) && $is_show!=-1){$where['is_show'] = $is_show;} //-1表示获取所有
  26. $where['pid'] = 0;
  27. $list = self::where($where)->orderBy('listorder', 'asc')->get();
  28. if($list)
  29. {
  30. foreach($list as $k=>$v)
  31. {
  32. $res[] = $v;
  33. $child = self::where(array('pid'=>$list[$k]->id,'is_show'=>self::IS_SHOW))->orderBy('listorder', 'asc')->get();
  34. if($child)
  35. {
  36. foreach($child as $key=>$value)
  37. {
  38. $res[] = $value;
  39. }
  40. }
  41. }
  42. }
  43. else
  44. {
  45. return false;
  46. }
  47. return $res;
  48. }
  49. public static function getOne($where)
  50. {
  51. return self::where($where)->first();
  52. }
  53. public static function add(array $data)
  54. {
  55. if ($id = self::insertGetId($data))
  56. {
  57. return $id;
  58. }
  59. return false;
  60. }
  61. public static function modify($where, array $data)
  62. {
  63. if (self::where($where)->update($data) === false)
  64. {
  65. return false;
  66. }
  67. return true;
  68. }
  69. //删除一条记录
  70. public static function remove($id)
  71. {
  72. if (!self::whereIn('id', explode(',', $id))->delete())
  73. {
  74. return false;
  75. }
  76. return true;
  77. }
  78. //删除一条记录
  79. public static function getWeixinMenuJson()
  80. {
  81. $where['pid'] = 0;
  82. $where['is_show'] = self::IS_SHOW;
  83. $list = self::where($where)->orderBy('listorder', 'asc')->get();
  84. $res='';
  85. if($list)
  86. {
  87. foreach($list as $k=>$v)
  88. {
  89. $child = self::where(array('pid'=>$list[$k]->id,'is_show'=>self::IS_SHOW))->orderBy('listorder', 'asc')->get();
  90. if($child)
  91. {
  92. $temp_child='';
  93. foreach($child as $key=>$value)
  94. {
  95. if($value->type == 'click')
  96. {
  97. $temp_child[] = array(
  98. 'type'=>$value->type,
  99. 'name'=>$value->name,
  100. 'key'=>$value->key
  101. );
  102. }
  103. elseif($value->type == 'view')
  104. {
  105. $temp_child[] = array(
  106. 'type'=>$value->type,
  107. 'name'=>$value->name,
  108. 'url'=>$value->key
  109. );
  110. }
  111. elseif($value->type == 'miniprogram')
  112. {
  113. $temp_child[] = array(
  114. 'type'=>$value->type,
  115. 'name'=>$value->name,
  116. 'url'=>$value->key,
  117. 'appid'=>$value->appid,
  118. 'pagepath'=>$value->pagepath
  119. );
  120. }
  121. }
  122. $res[] = array(
  123. 'name'=>$value->name,
  124. 'sub_button'=>$temp_child
  125. );
  126. }
  127. else
  128. {
  129. if($v->type == 'click')
  130. {
  131. $res[] = array(
  132. 'type'=>$v->type,
  133. 'name'=>$v->name,
  134. 'key'=>$v->key
  135. );
  136. }
  137. elseif($v->type == 'view')
  138. {
  139. $res[] = array(
  140. 'type'=>$v->type,
  141. 'name'=>$v->name,
  142. 'url'=>$v->key
  143. );
  144. }
  145. elseif($v->type == 'miniprogram')
  146. {
  147. $res[] = array(
  148. 'type'=>$v->type,
  149. 'name'=>$v->name,
  150. 'url'=>$v->key,
  151. 'appid'=>$v->appid,
  152. 'pagepath'=>$v->pagepath
  153. );
  154. }
  155. }
  156. }
  157. }
  158. return json_encode($res);
  159. }
  160. }